Monday, November 16, 2009

log4net in VS2008 desktop app

The log4net documentation advises one to modify app.config XML file to add a log4net stanza. However, under Visual Studio 2008, this causes an error message

Could not find schema information for the element ‘log4net’

There is a lot of advice on the web on how to avoid this compilation message for web apps, but not for desktop apps. The easiest way in a desktop app (and possibly also for a web app) is to replace in the C# code
XmlConfigurator.Configure()

with

XmlConfigurator.Configure(new MemoryStream(
(new System.Text.ASCIIEncoding()).GetBytes(
"<log4net>" +
" <appender name=\"ConsoleAppender\" type=\"log4net.Appender.ConsoleAppender\" >" +
" <layout type=\"log4net.Layout.PatternLayout\">" +
" <param name=\"Header\" value=\"[Header]\r\n\" />" +
" <param name=\"Footer\" value=\"[Footer]\r\n\" />" +
" <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %c %m%n\" />" +
" </layout>" +
" </appender>" +
" <root>" +
" <level value=\"DEBUG\" />" +
" <appender-ref ref=\"ConsoleAppender\" />" +
" </root>" +
"</log4net>")));

Or whatever was in your <log4net> stanza in your app.config

No comments: