Skip to content

superuser25/serilog-aspnetcore

 
 

Repository files navigation

Serilog.AspNetCore Build status NuGet Version

Serilog logging for ASP.NET Core. This package routes ASP.NET Core log messages through Serilog, so you can get information about ASP.NET's internal operations logged to the same Serilog sinks as your application events.

Instructions

First, install the Serilog.AspNetCore NuGet package into your app. You will need a way to view the log messages - Serilog.Sinks.Console writes these to the console; there are many more sinks available on NuGet.

Install-Package Serilog.AspNetCore -DependencyVersion Highest
Install-Package Serilog.Sinks.Console

Next, in your application's Program.cs file, configure Serilog first. A try/catch block will ensure any configuration issues are appropriately logged:

public class Program
{
    public static int Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .CreateLogger();

        try
        {
            Log.Information("Starting web host");
            CreateWebHostBuilder(args).Build().Run();
            return 0;
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Host terminated unexpectedly");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

Then, add UseSerilog() to the web host builder in BuildWebHost().

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseSerilog(); // <-- Add this line;
}

Finally, clean up by removing the remaining configuration for the default logger:

  • Remove calls to AddLogging()
  • Remove the "Logging" section from appsettings.json files (this can be replaced with Serilog configuration as shown in the EarlyInitializationSample project, if required)
  • Remove ILoggerFactory parameters and any Add*() calls on the logger factory in Startup.cs
  • Remove UseApplicationInsights() (this can be replaced with the Serilog AI sink, if required)

That's it! With the level bumped up a little you will see log output resembling:

[22:14:44.646 DBG] RouteCollection.RouteAsync
    Routes: 
        Microsoft.AspNet.Mvc.Routing.AttributeRoute
        {controller=Home}/{action=Index}/{id?}
    Handled? True
[22:14:44.647 DBG] RouterMiddleware.Invoke
    Handled? True
[22:14:45.706 DBG] /lib/jquery/jquery.js not modified
[22:14:45.706 DBG] /css/site.css not modified
[22:14:45.741 DBG] Handled. Status code: 304 File: /css/site.css

Tip: to see Serilog output in the Visual Studio output window when running under IIS, select ASP.NET Core Web Server from the Show output from drop-down list.

A more complete example, showing appsettings.json configuration, can be found in the sample project here.

Using the package

With Serilog.AspNetCore installed and configured, you can write log messages directly through Serilog or any ILogger interface injected by ASP.NET. All loggers will use the same underlying implementation, levels, and destinations.

Tip: change the minimum level for Microsoft to Warning and plug in this custom logging middleware to clean up request logging output and record more context around errors and exceptions.

Inline initialization

You can alternatively configure Serilog inline, in BulidWebHost(), using a delegate as shown below:

    // dotnet add package Serilog.Settings.Configuration
    .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
        .ReadFrom.Configuration(hostingContext.Configuration)
        .Enrich.FromLogContext()
        .WriteTo.Console())

This has the advantage of making the hostingContext's Configuration object available for configuration of the logger, but at the expense of recording Exceptions raised earlier in program startup.

If this method is used, Log.Logger is assigned implicitly, and closed when the app is shut down.

A complete example, showing this approach, can be found in the InlineIntializationSample project.

Enabling Microsoft.Extensions.Logging.ILoggerProviders

Serilog sends events to outputs called sinks, that implement Serilog's ILogEventSink interface, and are added to the logging pipeline using WriteTo. Microsoft.Extensions.Logging has a similar concept called providers, and these implement ILoggerProvider. Providers are what the default logging configuration creates under the hood through methods like AddConsole().

By default, Serilog ignores providers, since there are usually equivalent Serilog sinks available, and these work more efficiently with Serilog's pipeline. If provider support is needed, it can be optionally enabled.

Using the recommended configuration:

In the recommended configuration (in which startup exceptions are caught and logged), first create a LoggerProviderCollection in a static field in Program.cs:

        // using Serilog.Extensions.Logging;
        static readonly LoggerProviderCollection Providers = new LoggerProviderCollection();

Next, add WriteTo.Providers() to the logger configuration:

                .WriteTo.Providers(Providers)

Finally, pass the provider collection into UseSerilog():

                   .UseSerilog(providers: Providers)

Providers registered in Startup.cs with AddLogging() will then receive events from Serilog.

Using iniline initialization:

If inline initialization is used, providers can be enabled by adding writeToProviders: true to the UseSerilog() method call:

    .UseSerilog(
        (hostingContext, loggerConfiguration) => /* snip! */,
        writeToProviders: true)

Writing to the Azure Diagnostics Log Stream

The Azure Diagnostic Log Stream ships events from any files in the D:\home\LogFiles\ folder. To enable this for your app, first install the Serilog.Sinks.File package:

Install-Package Serilog.Sinks.File

Then add a file sink to your LoggerConfiguration, taking care to set the shared and flushToDiskInterval parameters:

    public static int Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console()
            // Add this line:
            .WriteTo.File(
                @"D:\home\LogFiles\Application\myapp.txt",
                fileSizeLimitBytes: 1_000_000,
                rollOnFileSizeLimit: true,
                shared: true,
                flushToDiskInterval: TimeSpan.FromSeconds(1))
            .CreateLogger();

About

Serilog integration for ASP.NET Core 2+

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 92.5%
  • PowerShell 6.5%
  • Shell 1.0%