'How can I enrich my logs with resource file key using serilog?

I have a configuration like this:

 logObj = new LoggerConfiguration()
               .MinimumLevel.ControlledBy(logLevel)
               .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
               .MinimumLevel.Override("System", LogEventLevel.Warning)
               .Enrich.WithExceptionDetails()
               .Enrich.FromLogContext()
               .Enrich.WithThreadId()
               .WriteTo.File(new Helpers.CompactJsonFormatter(),
               Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Logs\system_logs\" + StaticVariables.ActiveChannel.ToString()
               + @"\" + DateTime.Now.ToString("yyyy/MM/dd/") + ".txt",
               rollingInterval: RollingInterval.Hour,
               levelSwitch: StaticVariables.logLevel
               ).CreateLogger();

I want my log codes added by default in my logs. As example I have logs similar to these below:

  • ERR0007 — Detailed error message..
  • INF0128 — Some info log context..

Is it possible to parse these logs by default so I will have a property called "logCode" or something in my log output.

The main reason why I think I need this is for filtering and querying in case I need statistics of my logs.

Side note:

I take these logs from a resource file and below you can see a basic sample of it. I'm sharing this because maybe it is possible to take this property out of key instead of parsing the value?

Key Value
ERR0007 Detailed error message..
INF0128 Some info log context..

I'm also wondering if my approach is correct on this. Taking logs from a resource file and trying to implement the key as property or parsing the value to have the code from my template.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source