Archive for February, 2011

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.

Parser Error. User Control Cannot Be Found.

February 3, 2011

Hello Good Morning and Welcome.

Lovers of the ubiquitous .NET Parser Error are in for a real treat today. Fans of misogynistic Peruvian Biochemists will have to go elsewhere.

The issue was that my DogBreath UserControl could not be found at the path: ~/AU/Controls/DogBreath/DogBreath.ascx, which is the path I had put into the Register directive on the aspx page: correctly I might add.

There is in fact a DogBreath control at that path but the problem was there is also a Virtual Directory at AU, one level below the Virtual Directory for the website, exactly where I was expecting a Physical AU directory.

The Home (Physical) Directory of that virtual AU directory is C:\Inetpub\gutsache\staging. The physical (home) directory of my website was C:\Inetpub\Animals. So, whereas the expected path to my DogBreath control was C:\Inetpub\Animals\AU\Controls\DogBreath\DogBreath.ascx, the presence of that Virtual AU directory caused the parser to resolve to: C:\Inetpub\gutsache\staging\Controls\DogBreath\DogBreath.ascx.

No DogBreath control there, so hence the Parser Error. Lovely innit, fans ?

The quick workaround was to copy the controls to the correct place under the Virtual AU directory.

The real solution was to immerse a live stoat in a quart of boiling mercury rename the virtual AU directory to something else, anything else except AU, so it wouldn’t interfere with the Parser’s path resolution.

And if you didn’t quite catch that, if you get a Parser Error relating to a UserControl path not found, make sure that the top-level physical directory in your path isn’t also mapped to a Virtual Directory in IIS.

So that’s all we have time for here at Fun With Parsers. Until next week, then: Have a mercurified stoat on me.