I’m trying to get log4net working for a Windows service. It works fine for a console application.
I’ve created a blank service and the only thing it should do is write out a log statement to a file.
This is my main method.
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Info("Test");
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
This is my app.config file:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Test.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
I’ve also added this line to my AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
According to everything I’ve read online, this should be enough to create the log file in the directory where the service installed from. When this didn’t happen, I tried putting absolute directories as
log4net.Config.XmlConfigurator.Configure(new FileInfo("C:\\Users\\Public\\app.config"));
and
<file value="C:\Users\Public\Test.log" />
This didn’t work either. I thought maybe it was some crazy permissions problem since the service runs under a potentially restricted account, but I’ve just tried running it as an administrator and the file still isn’t created.
What am I doing wrong here?
This turned out to be a dependency issue with my setup project. The installer wasn’t installing the updated project each time, so the changes were being applied, but not installed.