Skip to content

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

Open
@jasong-au

Description

@jasong-au

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 from microsoft, but I don't want to have to rewrite all my services and logging (Using ILogger.Info instead of ILogger.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 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions