Log4net and windows service. What else should I do to make this work

女生的网名这么多〃 提交于 2019-12-10 09:44:11

问题


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:/


回答1:


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.




回答2:


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




回答3:


I've had success with log4net + windows service, you can you enable log4net internal debugging (http://log4net.sourceforge.net/release/1.2.0.30316/doc/manual/faq.html#internalDebug) and run your service in console mode to view any possible misconfiguration

try this as well:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]


来源:https://stackoverflow.com/questions/6918224/log4net-and-windows-service-what-else-should-i-do-to-make-this-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!