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.
Share This