liveBook · Manning

ClearProviders

As explained in Building Web APIs with ASP.NET Core

ClearProviders is a logging-builder method that removes the currently configured logging providers.

ClearProviders removes every logging provider currently configured on the logging builder. WebApplication.CreateBuilder initially supplies Console, Debug, EventSource, and Windows-only EventLog providers, so clearing comes before adding back only the providers the application should retain. section 7.2.4

var builder = WebApplication.CreateBuilder(args);
builder.Logging
.ClearProviders()   #1
.AddSimpleConsole()#2
.AddDebug();          #3

Use ClearProviders first, then chain AddSimpleConsole and AddDebug to create an explicit provider configuration. section 7.2.4

Keeping selected providers

In Program.cs, clear the defaults, add the simple console provider, and add the debug provider. Launch the application in Debug mode, execute BoardGamesController.Get, and inspect the hosting command prompt to verify the custom log message. section 7.2.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains the removal-and-readdition sequence, then stops short of provider internals.
Examples
Uses Console, Debug, EventSource, EventLog, and BoardGamesController.Get to demonstrate selection and verification.
What it assumes
Assumes familiarity with Program.cs, WebApplication.CreateBuilder, and basic logging configuration.
What you can do after
Operate/configure logging by removing defaults and explicitly adding the providers the application should keep.
How it connects
Connects logging providers with ILoggingBuilder, WebApplicationBuilder, and appsettings.json configuration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.