This requires a third party framework, namely Serilog, but I've nonetheless found it to be a very smooth experience with getting output to some place I can see it.
You first need to install Serilog's Trace sink. Once installed, you need to set up the logger like this:
Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Trace()
.CreateLogger();
(You can set a different minimum level or set it to a config value or any of the normal Serilog functionality. You can also set the Trace
logger to a specific level to override configs, or however you want to do this.)
Then you just log messages normally and they show up in your Output window:
Logger.Information("Did stuff!");
This doesn't seem like such a big deal, so let me explain some additional advantages. The biggest one for me was that I could simultaneously log to both the Output window and the console:
Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Trace()
.WriteTo.Console(standardErrorFromLevel: LogEventLevel.Error)
.CreateLogger();
This gave me great flexibility in terms of how I consumed output, without having to duplicate all my calls to Console.Write
with Debug.Write
. When writing the code, I could run my command line tool in Visual Studio without fear of losing my output when it exited. When I had deployed it and needed to debug something (and didn't have Visual Studio available), the console output was readily available for my consumption. The same messages can also be logged to a file (or any other kind of sink) when it's running as a scheduled task.
The bottom line is that using Serilog to do this made it really easy to dump messages to a multitude of destinations, ensuring I could always readily access the output regardless of how I ran it.
It also requires very minimal set up and code.
Debug.Write
in a library, and you build the library in release mode (which is often the case with nuget packages), then it won't log even if you run your tests/application in debug mode. I would suggestTrace.Write
as an alternative – Rob