Reputation: 612
I have a net core 7.0 application with Serilog library for logging. I've defined different log levels. For example, those errors which are fatal, critical will be reported using the slack sink, so the developers will receive the error in their slack. Then we have the daily operations from users that need to be persisted in a database. In my case I'm using the MSSqlServer sink, so I can save them in a table. Finally I'm using MSSqlServer sink again for logging users that enter and exit the system. In this case I need to persist this in a different table within the same database.
Here's what I did.
I create some extension methods so I can wire them up with the WebApplicationBuilder.
public static Serilog.ILogger CreateAuditLogger(this WebApplicationBuilder builder)
public static Serilog.ILogger CreateSessionLogger(this WebApplicationBuilder builder)
public static Serilog.ILogger CreateLoggerSlack(this WebApplicationBuilder builder)
Then I load them from program.cs
The thing is that Log.Logger is a global shared logger, so calling these methods will override the global logger with the last extension method that was invoked.
for instance, the code below will create the logger (I ommited the details about provided sinks...)
Log.Logger = new LoggerConfiguration().CreateLogger()
Is there a way to register global loggers just the way it is the the globally shared logger ? I would like to have one of it for slack, another for MSSqlServer sessions and MSSqlServer audits
I've been reading something about sublogger, but I think It'll be quite complex to add all these conditions to filter the correct logging in each case.
https://nblumhardt.com/2016/07/serilog-2-write-to-logger/
Upvotes: 0
Views: 907
Reputation: 612
I solved it by chaining the loggers like this
Log.Logger = new LoggerConfiguration()
.WriteTo.Logger(lc =>
lc.Filter.ByIncludingOnly(Matching.WithProperty("Type", "A1"))
.WriteTo.MSSqlServer(
connectionString: _connectionString,
sinkOptions: sinkOptionsAudit,
columnOptions: columnOptionsAudit))
.WriteTo.Logger(lc =>
lc.Filter.ByIncludingOnly(Matching.WithProperty("Type", "B2"))
.WriteTo.MSSqlServer(
connectionString: _connectionString,
sinkOptions: sinkOptionsSession,
columnOptions: columnOptionsSession))
.WriteTo.Conditional(e => e.Level == LogEventLevel.Error
|| e.Level == LogEventLevel.Fatal,
wt => wt.Slack(new SlackSinkOptions
{
WebHookUrl = "slackapi/messages",
PropertyOverrideList = new HashSet<OverridableProperties>()
{
OverridableProperties.CustomIcon,
OverridableProperties.CustomUserName
}
}))
.CreateLogger();
I encapsulate this in an extension method in program.cs. Then I created a static class called LoggerHelper with some methods.
public static void EmitFatalSlack(string stackTrace)
{
Log.Fatal($"{stackTrace}");
}
public static void EmitCriticalSlack(string stackTrace)
{
Log.Error($"{stackTrace}");
}
public static void EmitAudit()
{
Log.Information("some message");
}
So whenever I need to log messages to a database or slack I use those methods.
Upvotes: 0