Enrich Seq log statements with properties without also including them in the message line (using Serilog)

1.8k views Asked by At

I am using Serilog with Seq and want to enrich the logging that appears in Seq with my own properties.

If I enter a log statement like...

Log.Information("ProcessCycle {Site} {Activity}", SiteName, ActivityName);

In Seq I get...

enter image description here

Notice the Site and Activity values are shown as enriched properties in Seq, but they are also displayed in the overall message.

How can I log where I get enriched properties, but not have the values appear in the text message line? Notice I have the NuGet package that adds a ThreadId to each call. I want the Site and Activity properties to be in the list of enriched props, but not necessarily printed in the message line.

The answer for this might also require an understanding of our application.

The application is a windows service that spawns multiple activities that do different things. So the windows service orchestrates the various activities contained within. On a schedule it simply calls 'Process' on each activity to go off and do some work. Each time Process is called by the orchestrater, I need all logging by that Activity to automatically include the Site and Activity values as shown above (along with many more property values, but I don't want it all printed in the message line).

So instead of the above entry, instead we would see... Notice the message now reads just "ProcessCycle".

enter image description here

1

There are 1 answers

1
Ruben Bartelink On BEST ANSWER
Log.Information("ProcessCycle {Site} {Activity}", SiteName, ActivityName);

Needs to be changed to:

Log.ForContext("Site",SiteName)
    .ForContext("Activity",ActivityName)
    .Information("ProcessCycle")

To render as you desire.

You can also do Enrich.FromLogContext and do LogContext.PushProperty to do it more globally (search for those two strings to find an example).

Added by John Livermore

More information on FromLogContext and other methods can be found at... https://nblumhardt.com/2016/08/context-and-correlation-structured-logging-concepts-in-net-5/

FromLogContext creates an ILogger that can be used in scope for subsequent logging calls.