Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jasong-au opened this issue Nov 5, 2024 · 1 comment

Comments

@jasong-au
Copy link

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.

@nblumhardt
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants