Log4net和Windows服务.我还应该做些什么才能使这项工作 [英] Log4net and windows service. What else should I do to make this work

查看:84
本文介绍了Log4net和Windows服务.我还应该做些什么才能使这项工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,我想在Windows服务上运行log4net.这很简单-不可能完成任务.

Please help me, I want to run log4net on my windows service. And it is simply - mission impossible.

首先,使用Win表单-表现出色.

First of all, with Win forms - work great.

这是我对Windows服务的处理方式

Here is what I do with windows service:

添加到程序集:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

我的配置:

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

我的课:

public class LogProvider
{
    private ILog _Log;


    public LogProvider(string className)
    {
        XmlConfigurator.Configure();
        _Log = LogManager.GetLogger(className);
    }

    public void Info(object message)
    {
        _Log.Info(message);
    }
}

在主程序中:

using log4net;
using log4net.Config;

(...)

private LogProvider _logProvider;

(...)

                _logProvider = new LogProvider("Test");
                _logProvider.Info("asds");

怎么回事?

如果可能的话,请向我展示示例项目-因为上面的代码大约有100个版本或更多.我开始相信使用Windows服务无法使用log4net.

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.

更新:

因此,关于权限: 实际上,我有一个关于权限的例外.我用这一行(requirePermission ="false")修复了这个问题:

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();

    }
}

现在,我真的很困惑,我创建了另一个Windows服务,并且该服务可以正常工作-未经许可即行.天哪,这很奇怪.

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: 好,知道了我不知道这是怎么发生的,但我在app.config中找到了这一行:

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>

我正在研究15个子项目的程序,说真的,我不知道它来自哪里:/

I'm working on 15-subproject program and seriously, I don't know where it came from:/

推荐答案

我们遇到了问题,因为服务运行时目录为C:\Windows\SystemC:\Windows\System32.因此,log4net.config必须位于您不希望的系统目录中,或者必须提供log4net.config文件的路径.

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.

这篇关于Log4net和Windows服务.我还应该做些什么才能使这项工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆