There are a lot of questions/answers regarding configuring log4net and I have been looking at them for a while. Time to ask my own question. My problem is that my "Web api" web service does not create log files. I want to create log files at: C:\Logs. I have double checked that I have permission to write to this folder. In addition, we use log4net for our functional tests and when I run the tests, the file FunctionalTests.log is created. Therefore, I think we can rule out permissions.
What am I doing wrong here?
I have this line in my AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
I call XmlConfigurator.Configure() when my application starts. In my debugger I can see that Application_Start is hit and the Configure() method invoked:
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
XmlConfigurator.Configure();
HibernateConfig.InitHibernate();
}
Here is my Log4Net.config file, located at the root level of my project:
<log4net debug="true">
<!-- Level 1 -->
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\Logs\WebAPI.log"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="1000MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %property{X-CLIENT-REQUEST-ID} %property{X-REQUEST-ID} [%t] %-5p %c %m%n"/>
</layout>
</appender>
<appender name="RequestLogAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\Logs\WebAPIRequests.log"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="1000MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %property{X-CLIENT-REQUEST-ID} %property{X-REQUEST-ID} [%t] %-5p %c %m%n"/>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<logger additivity="false" name="Request">
<level value="INFO" />
<appender-ref ref="RequestLogAppender" />
</logger>
</log4net>
Configure
, and, in fact, because it's so unreliable as to when (if) log4net can process it, I recommend not relying on it altogether. Just callXmlConfigurator.ConfigureAndWatch()
from yourApplication_Start
. – Jeroen MostertXmlConfigurator.ConfigureAndWatch()
fromApplication_Start
should be the way to go – Alberto Rechy