Serilog DI in ASP.NET Core, which ILogger interface to inject?

Choosing which interface to use within your application is a matter of taste, really. If you prefer Serilog’s ILogger shorter method names (e.g. log.Error vs log.LogError), go with that, otherwise use the Microsoft’s generic ILogger<>. You have control over all the dependencies you use in your own projects, so there’s no strong technical reason to … Read more

Serilog – Output/Enrich All Messages with MethodName from which log entry was Called

in case you need a version in C#: public static class LoggerExtensions { public static ILogger Here(this ILogger logger, [CallerMemberName] string memberName = “”, [CallerFilePath] string sourceFilePath = “”, [CallerLineNumber] int sourceLineNumber = 0) { return logger .ForContext(“MemberName”, memberName) .ForContext(“FilePath”, sourceFilePath) .ForContext(“LineNumber”, sourceLineNumber); } } use like this: // at the beginning of the class … Read more

Serilog MSSQL Sink doesn’t write logs to database

Below are some ideas that could help you troubleshoot: Are you testing with Verbose or Debug events only? That could be the reason. You didn’t specify a global minimum level for Serilog (you only specified for the minimum level for the sink, which acts as a filter), and the default minimum is Information, which means … Read more

Serilog – multiple log files

I use the following configuration and it works for me: Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.LiterateConsole() .WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information).WriteTo.RollingFile(@”Logs\Info-{Date}.log”)) .WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug ).WriteTo.RollingFile(@”Logs\Debug-{Date}.log”)) .WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning ).WriteTo.RollingFile(@”Logs\Warning-{Date}.log”)) .WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error ).WriteTo.RollingFile(@”Logs\Error-{Date}.log”)) .WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Fatal ).WriteTo.RollingFile(@”Logs\Fatal-{Date}.log”)) … Read more