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

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.


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: