Thursday, July 31, 2014

log4net: DebugAppender

The DebugAppender in log4net allows you to output messages to any “debug” listener.  There is another similar appender called OutputDebugStringAppender. The difference is that OutputDebugStringAppender uses unmanaged code to call the Windows Debug API.  If you are running through the Visual Studio debugger, the output will not be displayed unless you request display from Unmanaged Code.  With DebugAppender, the display will happen automatically because its managed code.

Use with Windows Service

One of the cool things you can do with the DebugAppender is use this in a Windows Service.  In your service, use debug statements liberally throughout your code going to the DebugAppender.  If you use TopShelf for your Windows Service (which allows you to run in console mode and windows service mode), then while running through Visual Studio, you will see you debug statements showing through the Debug Output Window.  Then, what’s really cool, is that when you deploy the application, you can run Microsoft’s DebugView (dbgview.exe) on the machine where the service is running and you can AGAIN see the debug messages in a LIVE environment.  If debug view is not running, then the overhead on the live application should be minimal.

Visual Studio “swallows” Debug Messages


Note that if you want to see debug messages in DebugView while running through Visual Studio, you will have to “Debug >> Run without Debugging”.  If the Visual Studio debugger is running, then it EATS the debug message and DebugView will never “see” the message.

No comments:

Post a Comment