How to use Serilog in .NET Core Console app

I’m not sure about builder.SetMinimumLevel (it doesn’t use the Serilog enum). We set the logger level when creating the LoggerConfiguration object. Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Debug) // restricted… is Optional (…) .CreateLogger(); BTW. It’s worth pointing out the following section from Configuration Basics Logger vs. sink minimums – it is important to realize … Read more

How to add my own properties to Serilog output template

If you want to add properties that aren’t part of the message template, then you need to enrich the log context. That means adding the FromLogContext enricher, and adding your properties to your logged event a bit differently. Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Information() .WriteTo.Console(outputTemplate: “[{Timestamp:HH:mm:ss} {Level:u3}] {UserId} {Event} – {Message}{NewLine}{Exception}”) .CreateLogger(); using (LogContext.PushProperty(“UserId”, “123”)) … Read more

How to configure and use Serilog in ASP.NET Core 6?

You’ll need to make sure you have the following packages installed: Serilog Serilog.Extensions.Hosting (this provides the .UseSerilog extension method. If you have the Serilog.AspNetCore package, you do not need to explicitly include this) Then you’ll need a using: using Serilog; Which should allow you to access .UseSerilog via builder.Host: using Serilog; var builder = WebApplication.CreateBuilder(args); … 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

Serilog : Log to different files

You can do this by first making sure the performance counter events are tagged with either a particular property value (OpenMappedContext() in LibLog) or from a particular type/namespace. var log = LogProvider.For<MyApp.Performance.SomeCounter>() log.Info(…); When configuring Serilog, a sub-logger with filter applied can send just the required events to the second file. Log.Logger = new LoggerConfiguration() … Read more

Can a Serilog.ILogger be converted to a Microsoft.Extensions.Logging.ILogger?

Apart from installing Serilog.AspNetCore you could also install Serilog.Extensions.Logging and use these lines of code. This will give you some ideas how UseSerilog() works under the hood (well, more or less this way :D). var serilogLogger = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Verbose() .WriteTo.Debug() // Serilog.Sinks.Debug .CreateLogger(); var microsoftLogger = new SerilogLoggerFactory(serilogLogger) .CreateLogger<IMyService>(); // creates an instance … Read more

C# ASP.NET Core Serilog add class name and method to log

I solved this issue by using a combination of Jordan’s answer and this answer. I changed my Loggerconfiguration by adding the logcontext through enrichment and I added the property ‘method’ to my outputTemplate: var logger = new LoggerConfiguration() .MinimumLevel.Verbose() .MinimumLevel.Override(“Microsoft”, LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.RollingFile(Configuration.GetValue<string>(“LogFilePath”) + “-{Date}.txt”, LogEventLevel.Information, outputTemplate: “{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{Method}) {Message}{NewLine}{Exception}”) .CreateLogger(); The … Read more