Implementing logging providers

So far, we have defined what to log in our application; in this section, we will illustrate how to do this. In ASP.NET Core, the logging provider is initialized using dependency injection. Moreover, it is possible to conditionally initialize it, depending on the environment or other startup options.

ASP.NET Core provides some built-in logging providers, as shown in the following table:

Providers Namespace Description
Console  Microsoft.Extensions.Logging.Console
Sets the console of the running application as the output of the logs.
Debug  Microsoft.Extensions.Logging.Debug
Writes messages in the debug output window when a debugger is attached.
EventSource  Microsoft.Extensions.Logging.EventSource
Sets the Windows ETW as the main log's output.
EventLog Microsoft.Extensions.Logging.EventLog
Sets the Windows event log as the main log's output.
TraceSource Microsoft.Extensions.Logging.TraceSource
To use this provider, ASP.NET Core needs to run on the .NET Framework. The application will use the listeners that are provided by the trace source.

 

It is crucial to note that all the logging providers are complementary. Furthermore, it is possible to add many of them so that we can perform trace logging in more sources. We can configure the provider in the Startup class and in the Program class. Let's take a look at the Program class of Catalog.API:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace Catalog.API
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[]
args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
}

Here, the Program class uses WebHost.CreateDefaultBuilder to create the WebHostBuilder instance of the service. If we go further into the method, we will see that it uses the following syntax to define the log's provider:

...
.ConfigureLogging((Action<WebHostBuilderContext, ILoggingBuilder>) ((hostingContext, logging) =>
{
logging.AddConfiguration((IConfiguration)
hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
}))

As you can see, the Program class defines three built-in providers by default. Also, it uses the Configuration instance to pass the configurations that are described in the appsettings.json file.

Moreover, it is possible to override the default provider by explicitly calling the ConfigureLogging extension method in the Program class:

...
public class Program
{
...
public static IWebHostBuilder CreateWebHostBuilder(string[]
args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(builder =>
{
builder.[...]
})
.UseStartup<Startup>();
}
}

The preceding snippet is useful if we want to add a custom logging provider to our application. ASP.NET Core also provides us with a convenient way to configure the logging provider in the Startup class: it is possible to use the AddLogging extension method in ConfigureServices:


public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
...
services.AddLogging(builder => builder.AddConsole());
}

...
}
}

The preceding snippet initializes the logging services on the execution of ConfigureServices in the Startup class. The initialization of the logging provider at the Startup level is useful when we want to initialize the log provider in terms of the environment or a specific configuration flag. Let's proceed by learning how to implement a custom logging provider.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.101.60