Please help me, I want to run log4net on my windows service. And it is simply - mission impossible.
First of all, with Win forms - work great.
Here is what I do with windows service:
Add to assembly:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
My config:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c: %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
My class:
public class LogProvider
{
private ILog _Log;
public LogProvider(string className)
{
XmlConfigurator.Configure();
_Log = LogManager.GetLogger(className);
}
public void Info(object message)
{
_Log.Info(message);
}
}
And in main program:
using log4net;
using log4net.Config;
(...)
private LogProvider _logProvider;
(...)
_logProvider = new LogProvider("Test");
_logProvider.Info("asds");
WHAT IS GOING ON ?
If it possible, please show me sample project - because, this code above is about 100 version or more. I start to believe that this is impossible to use log4net using windows service.
UPDATE:
So, about permissions: Actually I get an exception, about permissions. I fix this with this line (requirePermission="false"):
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
Now, It still not working, with no exception.
This is my final exec code:
protected override void OnStart(string[] args)
{
try
{
LogProvider lp = new LogProvider("asd");
lp.Info("asd2");
}
catch (Exception ex)
{
System.IO.FileInfo _file;
System.IO.StreamWriter _writer;
_file = new System.IO.FileInfo("D:\\Log\\asdCatch.txt");
_writer = _file.CreateText();
_writer.WriteLine("[" + DateTime.Now.ToString() + "] Init Log");
_writer.WriteLine("[" + DateTime.Now.ToString() + "] " + ex.ToString());
_writer.Close();
}
}
Now I'm really confused, I've created another windows service and it's working - without that line with permission. OMG, this is weird.
UPDATE2: OK, I've got it. I don't know how did this happened, but I found that line in my app.config:
<runtime>
<NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>
I'm working on 15-subproject program and seriously, I don't know where it came from:/
We had a problem because the services runtime directory is C:\Windows\System
or C:\Windows\System32
. Therefore the log4net.config has to be in your system dir which is not desirable or you have to provide the path to your log4net.config file.
Most likely if this logging is working in a windows application it is a permissions issue. When you are logging in a windows service the user that the windows service is running under must have write permissions to the folder you are trying to log to.
Also you should try enabling log4net debugging to see if this helps you.
<log4net debug="true">
... configuration ...
</log4net>
You should also look at my answer in a post very similar to this if there are additional questions:
log4net doesn't create log file when deployed on IIS7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With