Archive for the ‘Debugging / Instrumentation’ Category

Log4Net Logs Not Generated, No AssemblyInfo.cs, WebSite Project

March 9, 2011

Today I had to implement Log4Net logging for my website. Using the lessons I had learned in my previous Journey Of Pain setting up log4net for a Console Application (see previous post) I figured this would be a doddle. Not so. Once again my logger failed to find any appenders.

Because my site is a VS WebSite Project and not a Web Project, it does not have an AssemblyInfo file, which is the normal place you would put your XmlConfigurator. So I put the call to XmlConfigurator.Configure in Global.asax Application_Start as suggested by Apache. No dice. Ot should I say once again, no Appenders and definitely no logging.

Googling hither and thither the core temperature of my liver reached Chernobyl proportions before I staggered blindly into the Googleistic revelation that log4net default configuration has a bug where AssemblyInfo is not present, as is the case for all Website projects (as opposed to Web Projects).

To overcome this bug you must move your log4net config to an external file and use the magic ApplicationSetting key ‘log4net.Config’ in web.config. The Stack Overflow contribuor Precipitous provided the necessary balm to my self-combusting liver in his post to Stack Overflow on the subject log4net in external file does not work.

The fix, in summary, create an external log4net config file, but leave the log4net section handler in your web.config, and add this Application Key add key=”log4net.Config” value=”your-path-here” .

Now relax, settle back, and watch this video of the Prime Minister Of Australia eating his ear wax.

Log4Net Will Not Write To Log File, Configuration System Failed To Initialize, .NET Console Application

February 6, 2011

Had a bit of pain fun getting log4net logging working….

I was putting together a .NET Console Application which would become the executable of a Scheduled Task to purge old records from the database on a daily basis, so I added a Console Project to my existing solution.

When you do that you don’t get an app.config, so I added a new one, which of course was blank, and then copied and pasted in the log4net section from an existing application. When my app started up I got the error ‘Configuration System Failed To Initialize’.

So I Googled around and found that I needed to add a SectionHandler for log4net to my app.config, by which I mean this:

section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” /
log4net configuration XML goes here

You can see a nicely laid out example here

But I still got ‘Configuration System Failed To Initialize’, so more Googling, a spot of plastic surgery to make me feel better, and I began to ken that I needed to call a log4net Configurator (lovely word that), so I put the following line first thing in Program.cs:


I was partially successful. ‘Configuration System Failed To Initialize’ went away, but no logs were generated. Inspection of my log4net appenders collection at runtime showed I did not have either the RollingFileAppender or the SmtpAppender I had defined in app.config. All I had was a ConsoleAppender which I didn’t want and didn’t need.

You too can examine your lack of appenders by coding this:

IAppender[] ar = log.Logger.Repository.GetAppenders();

The Beast You Need: XmlConfigurator

So, more rehab, more plastic surgery, (this time a rather nice epiglottal debone and cauterize) more Google until I became aware of the finalpiece in the puzzle: XmlConfigurator. That’s the beast you need to make log4net read the app.config.

And a nice way to do it is to tuck it away in AssemblyInfo.cs like so:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Many thanks to Apache’s Logging Services group who put up the log4net Manual Configuration page.