Wednesday, 24 August 2011

Direct log4net output to Visual Studio IDE output window

You can tell log4net to write its messages not only to files or DB but also to an Output window in Visual Studio. Very handy for debugging when you didn't want to switch between windows or query your database.
It appears that you need to tell log4net to use the Trace appender, which, according to the documentation, writes the events using the System.Diagnostics.Trace.Write(string,string) method.

Here is how to change the log4net section in app.config or web.config (if you keep yor log4net configuration in this file) - note this (LOGGER) in the pattern, it is there to distinguish the output from log4net from any other messages in Output window:
<log4net>
 
	<root>
		<level value="DEBUG" />
		<appender-ref ref="MyTraceAppender"/>
	</root>
	<appender name="MyTraceAppender" type="log4net.Appender.TraceAppender">
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="(LOGGER) %-5p %d [%t] %c %m%n"/>
		</layout>
	</appender>
 
</log4net>

2 comments:

  1. thank you for posting this, eyes are blurry and head hurts, this saved me!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete