Serilog topshelf integration not working
Asked Answered
M

1

15

I'm trying to set up a simple logging configuration for my Windows service using Topshelf and Serilog (the Serilog.Extras.Topshelf package respectively).

HostLogger.UseLogger(new SerilogHostLoggerConfigurator(new LoggerConfiguration()
                .WriteTo.RollingFile(AppDomain.CurrentDomain.BaseDirectory + "\\logs\\app-{Date}.log")
                .WriteTo.ColoredConsole()
                .MinimumLevel.Debug()
                .CreateLogger()));
HostFactory.Run(x => {
            x.UseSerilog();
            ...

The service runs fine, however no output is made, neither to the console nor the specified log file (I can see that one is being created but it remains empty). Has anyone experience using both frameworks?

Monoploid answered 3/2, 2015 at 12:29 Comment(0)
A
16

The second call "x.UseSerilog()" resets TopShelf's HostLogger to use Serilog's global instance (Log.Logger), which you have not configured.

Remove the second call and the logging should start working.

Another option is to configure the global logger:

Log.Logger = new LoggerConfiguration()
            .WriteTo.RollingFile(AppDomain.CurrentDomain.BaseDirectory + "\\logs\\app-{Date}.log")
            .WriteTo.ColoredConsole()
            .MinimumLevel.Debug()
            .CreateLogger();

HostFactory.Run(x => {
        // configure TopShelf to use Serilog's global instance.
        x.UseSerilog();
}
Anthelmintic answered 9/5, 2015 at 2:46 Comment(1)
+1 I didn't realize that using x.UseSerilog without the LoggerConfiguration or ILogger parameter would use the global logger.Carn

© 2022 - 2024 — McMap. All rights reserved.