Serilog: Register in different files

I log events of all types in a single Json file regardless of LogLevel. Now I have a requirement to write some custom performance counters to a separate Json file. How can this be done in Serylogue. Should I create another instance of the logger and use it, wherever I am going to register performance counters? Want to use this with liblog

+7
c # serilog
source share
1 answer

You can do this by first making sure that the performance counter events are marked with either a specific property value ( OpenMappedContext() in LibLog) or from a specific type / namespace.

 var log = LogProvider.For<MyApp.Performance.SomeCounter>() log.Info(...); 

When configuring Serilog, a sub-logger with a filter applied can send only the necessary events to the second file.

 Log.Logger = new LoggerConfiguration() .WriteTo.Logger(lc => lc .Filter.ByExcluding(Matching.FromSource("MyApp.Performance")) .WriteTo.File("first.json", new JsonFormatter())) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(Matching.FromSource("MyApp.Performance")) .WriteTo.File("second.json", new JsonFormatter())) .CreateLogger(); 
+2
source share

All Articles