How to use Serilog in .NET Core Console app
Asked Answered
D

2

33

I wanted my application to have capability of logging to a file, so I started to look for something more than default .NET Core 2.2 logging framework. I see that Serilog might do the job. However, I cannot find any document on how to setup Serilog in .NET Core Console application with Dependency Injection. All I see is ASP.NET materials, which is probably not what I need.

I started doing it myself. I installed (Nuget):

  • Serilog
  • Serilog.Extensions.Logging
  • Serilog.Sinks.File
  • Serilog.Sinks.Console (to use Serilog for all my logging)

I created an extension forServiceCollection

        public static void AddLogging(this IServiceCollection services, Microsoft.Extensions.Logging.LogLevel logLevel)
        {
            var serilogLogger = new LoggerConfiguration()
            .WriteTo.Console()
            .WriteTo.File("log.txt")
            .CreateLogger();

            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(logLevel);
                builder.AddSerilog(logger: serilogLogger, dispose: true);
            });
        }

Logging works, however:

  • log level is not what I set it to. It seems that serilog is using INFO level, although I wanted to have DEBUG. Why isn't my setting respected? After all, I'm still using NET Core's logging framework, so I'm using it to setup the log level
  • am I actually doing this setup correctly? I am not really sure if dispose should be true. Generally, I want NET Core's Dependency Injection framework to take care of disposal of services.
Driblet answered 11/10, 2019 at 10:29 Comment(1)
I would like to use Serilog to inject the logger as parameter in one class that is used by a console application. I guess that your example it is a good option, but i don't know how is created the services parameter that is passed to the AddLogger() method. Could you add the Main() of the console application too? Thanks so much.Imperceptive
A
15

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 that the logging level can only be raised for sinks, not lowered. So, if the logger's MinimumLevel is set to Information then a sink with Debug as its specified level will still only see Information level events. This is because the logger-level configuration controls which logging statements will result in the creation of events, while the sink-level configuration only filters these. To create a single logger with a more verbose level, use a separate LoggerConfiguration.


I'm not sure about builder.AddSerilog.

Here's what works for me.

using Serilog;

(...)

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    (...)
    .CreateLogger();

(...)

return new HostBuilder()
      .ConfigureHostConfiguration(...)
      .ConfigureServices(...)
      .UseSerilog();
Angloindian answered 11/10, 2019 at 12:32 Comment(1)
Your first point about configuration is valid also in my case. The builder.SetMinimumLevel settin has higher priorit than Serilog's setting. So, I can set Verbose logging on Serilog and the actual desired level on Microsoft logging to get what I need. I see you are in some hosted application, like ASP.NET. Probably you're using Serilog.AspNetCore package. In my case, I don't have any host.Driblet
O
2

In case you don't want to work with HostBuilder and instead use the serviceCollection to inject serilog skins, you can also use the library: Serilog.Extensions.Logging And do so:

using Serilog;
using Microsoft.Extensions.Logging;
...
Log.Logger = new LoggerConfiguration()
  .WriteTo.Console()
  .CreateLogger();
IServiceCollection services = new ServiceCollection();
services.AddLogging(builder =>
{
  builder.SetMinimumLevel(LogLevel.Debug);
  builder.AddSerilog(Log.Logger, true);
});
serviceProvider = services.BuildServiceProvider();
Orchard answered 14/12, 2023 at 8:23 Comment(1)
Ones when add only builder.AddSerilog without UseSerilog(), start working - for .Net Core 7 Console appElectrode

© 2022 - 2024 — McMap. All rights reserved.