2

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

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
John Livermore
  • 30,235
  • 44
  • 126
  • 216
  • Where _do_ you want the data to go/be ? Can you do a text markup rendering of what you'd prefer (i.e. I'm not getting whether you want to split sets of properties up or they were only for internal consumption in the first place etc) – Ruben Bartelink Nov 15 '18 at 07:35
  • I updated my question to include what we want the log entry to look like. Does that answer your question? – John Livermore Nov 15 '18 at 15:08

1 Answers1

5
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.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • 2
    also inplementing the ILogEventEnricher and passing it as parameter to FrimContext method may be useful for uniformly add properties to the messages from one "scope" – Chizh Nov 15 '18 at 22:29