PostSharp 6.5 / / Post­Sharp Documentation / Logging / Configuring Specific Logging Frameworks / Logging to Serilog

Logging to Serilog

This article shows how to use PostSharp Logging and Serilog together.

This topic contains the following sections:

Getting started with PostSharp Logging and Serilog

To use PostSharp Logging with Serilog:

  1. Add PostSharp logging to your codebase as described in Adding Detailed Logging to your Solution.

  2. Add the PostSharp.Patterns.Diagnostics.Serilog package to your startup project, as well as the packages for all Serilog sinks you will need. For the next code examples, install the packages Serilog.Sinks.ColoredConsole and Serilog.Sinks.File.

  3. In the application startup file, include the following namespace imports:

    using PostSharp.Patterns.Diagnostics;
    using PostSharp.Patterns.Diagnostics.Backends.Serilog;
    using Serilog;

    In the application startup method, include the following code:

    // The output template must include {Indent} for nice output.
    const string template = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Indent:l}{Message}{NewLine}{Exception}";
    // Configure a Serilog logger.
    var logger = new LoggerConfiguration()
      .WriteTo.File("serilog.log", outputTemplate: template)
      .WriteTo.ColoredConsole(outputTemplate: template)
    // Configure PostSharp Logging to use Serilog
    LoggingServices.DefaultBackend = new SerilogLoggingBackend(logger);

    This example code instructs Serilog to write all log records to a file named serilog.log and to the console.

    Note that this code snippet supplies a custom output template. By default, Serilog does not indent the logs generated by PostSharp. PostSharp passes the indentation string to Serilog as a parameter named Indent. In order to produce indented logs, you need to include the {Indent:l} parameter into your output template.

    See the Serilog documentation for more details.

If you run your application, you should now see a detailed log in a file named serilog.log. If your application is a console application, you should also see the log in the console.

Using Serilog formatters instead of PostSharp ones

By default, PostSharp Logging will use its own formatters to format parameter values into a string and will pass the already-formatted string to Serilog.

If you want the unformatted parameter value to be passed to Serilog instead of the string, set the SerilogLoggingBackendOptions.UseSerilogFormatters property to true.

Including more semantic parameters

By default, PostSharp Logging passes the following pieces of information to Serilog as semantic parameters: parameter values, return values, this value, and execution time. Other pieces of information such as for instance the type and method name are included in the message template.

If you want pass more pieces of information as semantic parameters, set the SerilogLoggingBackendOptions.SemanticParametersTreatedSemantically and SerilogLoggingBackendOptions.IncludedSpecialProperties properties.

Note Note

Semantic parameters have a relatively high performance cost in Serilog. We suggest you do not use more semantic parameters than necessary.