George
George

Reputation: 87

Why use .WriteTo.Debug in Serilog LoggerConfiguration()?

In the project I'm working on logger is initialized like this:

var logfile = String.Format($"logs/out_{Process.GetCurrentProcess().Id}_.log");
            var config = new LoggerConfiguration()
                //.Enrich.FromLogContext()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                .WriteTo.LoggerSink()
                .WriteTo.File(logfile, rollingInterval: RollingInterval.Minute)
                .WriteTo.Debug(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
                .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}");

            Log.Logger = config.CreateLogger();
            Log.Logger.Information("Startup()");

Which causes line duplication, so

Log.Logger.Information("Startup()");

results in

[18:06:02 DBG] Startup()
[18:06:02 DBG] Startup()

appearing in my VS Code Debug Console.

But only when debugger is attached.

I figured out that the issue can be resolved with removal of the line

.WriteTo.Debug(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")

But that is the purpose of it?

Upvotes: 2

Views: 2072

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131591

There's no duplication. Debug is a separate channel that writes to any attached debugger. Without an attached debugger, anything written to Debug is lost. This way, there's no overhead if nothing is listening. Most IDEs display anything written to the Debug channel in the same window they use for Console output.

When you run the application without debugging, only messages written to Console will appear in the console. You can view the debut output without attaching the debugger if you use a tool like DebugView

Upvotes: 6

Related Questions