Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog topshelf integration not working

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?

like image 403
xvdiff Avatar asked Feb 03 '15 12:02

xvdiff


1 Answers

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();
}
like image 74
Jaben Avatar answered Sep 20 '22 21:09

Jaben