This perplexing error message recently started to occur in .NET 2.0 applications when adding log4net information within the web.config file as shown in the documentation. The source of the problem seems to be related to the visual studio XML validation and isn’t specifically a log4net problem. More information about the error can be found on this MSDN thread. The only solution that seems to have worked for anyone is to uninstall and re-install WinFX (.NET 3.0 framework).
This error is particularly frustrating because there are hundreds of example configurations for log4net posted online. However many of them are using older versions of both log4net and .NET itself which is not always mentioned. As it happens, there’s another way to configure log4net which doesn’t solve the web.config issue, but is very easy to implement. This configuration works using .NET 2.0 and log4net 1.2.10. Instead of putting the configuration in web.config, you just create an ordinary xml file with your log4net configuration and initialize the logger from this file instead of web.config. This has an added advantage that you can modify the logging settings without having to modify web.config (which typically restarts the application). The negative to this method is that file permissions must be set to allow read access to this config file.
To configure log4net this way:
First create a new xml file. The name is not important, but the file permissions must be set so that the IIS worker process can read it. Put your <log4net>…</log4net> code that which would normally go in web.config in this file. An example of this can be found at this page (scroll to the section “Reading Files Directly).
If you don’t have a Global.asax file, create one. Within the Application_Start event handler method, place the following code:
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("log4net.xml")));
For the sake of simplicity, the logger file in the example above is called log4net.xml and is located in the root of the web application. For security reasons you may prefer to put this somewhere else. If someone knows the location of this file and can access it through the browser, they will be able to see your configuration which will show the location of your debug output. Worse yet, if you are logging to a DB your connection string will likely be in there.
Note that XmlConfigurator.Configure is used instead of the deprecated DOMConfigurator.Configure (per the docs as of log4net 1.2.10). This will load the logger settings once on application startup. You can optionally use XmlConfigurator.ConfigureAndWatch instead which will tell log4net to watch this file for configuration changes. Obviously there would be some overhead in doing this.
One additional thing worth mentioning is that log4net will not complain at all if any of the file paths you give it do not exist or do not have the proper permission. It will simply just not log anything. This can be frustrating as well so be sure to carefully check all file paths and permissions.
I hope this post may have saved you some time and frustration. If you have any additional information, please feel free to post a comment.
Hi Jason,
Thank you very much for posting this nice article.
I was struggling to implement log4net but I was getting the error that “Error:Unrecognized configuration section log4net” when I placed log4net section in web.config file, and this issue resolved simply after following the steps you have provided. Thanks once again.
Hey Sudhir – I’m glad it saved you some grief! Thanks for stopping by.
thanks for this article. it really helped me to fix the problem and mine is working great with net 2.0 and log4net 1.2.10
Hi Jason, very useful post for people who are new to log4net. Thanks.
Thanks for the article. Another solution that worked for me is to specify the fully qualified path of the web.config
So, syntax would be the following:
log4net.Config.DOMConfigurator.Configure(new System.IO.FileInfo(Server.MapPath(“web.config”)));
Thanks, still, I’d like to be able to use the regular config file
Your suggestions, while appreciated, actually didn’t work for me. I found the answer here on .
Just posting that here in the hopes that it is useful to someone else.
Sorry about that messed up my tags. Oh well, the link still works and I can’t edit it.
Thanks JFM – I like the solution you posted, I’ll probably give that a try, it seems like possibly a cleaner approach.
Hi, I am working on a windows service using VS 2008 with .net FW 3.5,
I was having troubles making log4net work, until i did the following changes:
in the app.config file i had:
and i was having this message:
Could not find schema information for the element ‘log4net’
and Also when i try to start the service, the logfile is being created, but empty and I was having an Exception:
System.TypeInitializationException
then i checked the log4net samples and i changed the app.config to :
I am still having that message:
Could not find schema information for the element ‘log4net’
BUT! the log file is being created and the I am not having any exceptions, the windows service is starting and running normally!
Hope this helped!
it seems that the code was not pasted.
so in the old config file i had:
section name=”log4net” type=”System.Configuration.IgnoreSectionHandler”
in the new one i changed it to:
section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler
thanks ghinwa!