Configure Log4Net in web application
I have this code and the config file below:
ILog log = LogManager.GetLogger(typeof(MyClass));
log.Debug("Testing");
TestProj
directory is not created and if I create it, no TestLog.txt
file, no log ... nothing.
Any idea?
Thanks,
The config file
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\TestProj\\TestLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
You need to call the Configure
function of the XmlConfigurator
log4net.Config.XmlConfigurator.Configure();
Either call before your first loggin call or in your Global.asax like this:
protected void Application_Start(Object sender, EventArgs e) {
log4net.Config.XmlConfigurator.Configure();
}
Another way to do this would be to add this line to the assembly info of the web application:
// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Similar to Shriek's.