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?

xvdiff
  • 2,179
  • 2
  • 24
  • 47

1 Answers1

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();
}
Jaben
  • 426
  • 3
  • 9
  • +1 I didn't realize that using `x.UseSerilog` without the `LoggerConfiguration` or `ILogger` parameter would use the global logger. – Jeff Aug 05 '16 at 15:36