Serilog: Contextual logging with log context enricher not working

Created on 12 Jun 2018  路  3Comments  路  Source: serilog/serilog

I've configured the logger this way:

Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .WriteTo.File(logsFile, rollingInterval: RollingInterval.Day)
                .CreateLogger();

then used this code to create a contextual logger in my POCO classes:

private readonly ILogger _logger = Log.ForContext<Site>();

I am not seeing any contextual information in my log files. Does the file sink not support the contextual info or something else is wrong?

Most helpful comment

Hi,

You are using the default output format for the File sink, which is
{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}.

This means that by default, you will see the rendered message + properties ({Message}) but not attached properties (like contextual information) that are not part of the log message template.

You can use the {Properties} element in the output format to show remaining properties that are not used in the log message template. This is documented here : https://github.com/serilog/serilog/wiki/Formatting-Output#formatting-plain-text

If you try :

Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .WriteTo.File(logsFile, 
                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}{NewLine}{Properties}"
                    rollingInterval: RollingInterval.Day)
                .CreateLogger();

You should see the extra properties (like SourceContext or values from LogContext).
You can also change formatting of the {Properties} to {Properties:j} for JSON-formatting of the properties.

I hope this helps !

All 3 comments

Hi,

You are using the default output format for the File sink, which is
{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}.

This means that by default, you will see the rendered message + properties ({Message}) but not attached properties (like contextual information) that are not part of the log message template.

You can use the {Properties} element in the output format to show remaining properties that are not used in the log message template. This is documented here : https://github.com/serilog/serilog/wiki/Formatting-Output#formatting-plain-text

If you try :

Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .WriteTo.File(logsFile, 
                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}{NewLine}{Properties}"
                    rollingInterval: RollingInterval.Day)
                .CreateLogger();

You should see the extra properties (like SourceContext or values from LogContext).
You can also change formatting of the {Properties} to {Properties:j} for JSON-formatting of the properties.

I hope this helps !

Hello is there any way to display only certain properties ?
Can you do something like
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}{NewLine}{Properties : SomePropertyOnly }"

@sanzor main tool at your disposal is to plug in an Enricher that does a RemoveProperty before you reach that point in the emission chain (about to be wrapped) ETA: see the gitter chat for similar discussion over last week

Was this page helpful?
0 / 5 - 0 ratings