serilog-extensions-logging icon indicating copy to clipboard operation
serilog-extensions-logging copied to clipboard

Can't figure out how to use Serilog with .NET console app and ILogger<T>

Open jasong-au opened this issue 1 year ago • 1 comments
trafficstars

I don't know how to better explain it, I've tried a lot of things. Eventually I got it working by injecting Serilog.ILogger instead of the ILogger<T> from microsoft, but I don't want to have to rewrite all my services and logging (Using ILogger.Info instead of ILogger<T>.LogInformation for instance). This seems to be possible using ASP.NET core with builder.UseSerilog()? But that doesn't exist. In the end I had to do it manually:

        var builder = Host.CreateApplicationBuilder(args);

        var logger = new LoggerConfiguration()
            .ReadFrom.Configuration(config)
            .CreateLogger();

        builder.Services.AddSingleton<Serilog.ILogger>(logger);

Nothing else I tried worked. There is a snippet of logging configuration with ClearProviders() and then adding Serilog as a provider. When I debugged my service I saw the ILogger<T> then had just the Serilog provider instead of the default 4 ILogger providers (console, event log, etc.), but it didn't work. Is there a way to get this to work? Some sample or information int he readme would be helpful. It's frustrating because there is no error, it just doesn't show any output. I don't know then if I have my Serilog section in app settings correct, or what the problem is.

jasong-au avatar Nov 05 '24 21:11 jasong-au