简体   繁体   English

log4net无法在Windows服务中运行

[英]log4net not working in a windows service

I have a console app which I am converting into a windows service. 我有一个控制台应用程序,我正在转换为Windows服务。 As a console app my log4net logging is working fine. 作为控制台应用程序,我的log4net日志记录工作正常。 But converting it into a windows service, my log4net logging has stopped working. 但是将它转换为Windows服务,我的log4net日志记录已停止工作。

I have added this to my assemblyInfo.cs in the service project: 我已将此添加到服务项目中的assemblyInfo.cs中:

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

This is my service class with onstart and onstop: 这是我的onstart和onstop服务类:

private static log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private Builder _builder;

        public YCSWebServerService()
        {
            InitializeComponent();
            _builder = new Builder();
        }

        protected override void OnStart(string[] args)
        {
            _log.Info("YCSWebServerService started");
            _builder.Start();
        }

        protected override void OnStop()
        {
            _log.Info("YCSWebServerService stopped");
            _builder.Stop();
        }

I have a "specific" log4net config file added to my service project: 我有一个“特定的”log4net配置文件添加到我的服务项目:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="EventLogAppender" />
    </root>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <threshold value="DEBUG" />
      <applicationName value="Lantic YCS WebServer" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="(%-5level %date{hh}:%date{mm}:%date{ss} [%thread] %logger [%property{NDC}] ) %message %n" />
      </layout>
    </appender>
  </log4net>

</configuration>

Any ideas or tips? 任何想法或提示?

Did you just add a log4net section to your app.config file? 您刚刚在app.config文件中添加了log4net部分吗? In your question you mentioned that you have "specific log4net config file", but the sample you gave looks like the whole contents of app.config. 在您的问题中,您提到您有“特定的log4net配置文件”,但您提供的示例看起来像app.config的全部内容。 If app.config is the case then you could have simpler string in AssemblyInfo.cs: 如果是app.config,那么你可以在AssemblyInfo.cs中使用更简单的字符串:

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

Also adding requirePermission="false" to the section in my app.config helped me when I fixed similar problem with log4net not logging to file for Windows Service (see extra attribute - requirePermission="false"): 另外在我app.config中的部分添加requirePermission =“false”帮助我解决了log4net没有记录到Windows服务文件的类似问题(请参阅额外属性 - requirePermission =“false”):

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
  </configSections>

Some things to try: 有些事要尝试:

Does the user running the service have write permissions? 运行该服务的用户是否具有写权限?

Are any exceptions getting logged to the windows logs? 是否有任何异常记录到Windows日志中?

Have you tried running the service in debug mode to see if Log4net is throwing any exceptions? 您是否尝试在调试模式下运行该服务以查看Log4net是否抛出任何异常?

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=293617 https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=293617

http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/5bef59bc-a28f-4e6d-8ddb-730e12764162 http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/5bef59bc-a28f-4e6d-8ddb-730e12764162

In Windows Vista, Windows XP SP 2, Windows Server 2003, 2008 and Windows 7 a user needs Administrator rights to access the Security Log. 在Windows Vista,Windows XP SP 2,Windows Server 2003,2008和Windows 7中,用户需要管理员权限才能访问安全日志。 Now when log4net tries to create a Event Log Source , all Logs are checked if it already exists. 现在,当log4net尝试创建事件日志源时 ,将检查所有日志是否已存在。 So with a Windows Service, the user of the Windows Server would need Administrator rights (which is not good). 因此,对于Windows服务,Windows Server的用户需要管理员权限(这不是很好)。

Solution: configure a explicit Source in the log4net configuration (as you did: <applicationName value="Lantic YCS WebServer" />, Lantic YCS WebServer is your Source) and create this Source in you Setup (because Setup-User should have Administrator rights). 解决方案:在log4net配置中配置显式Source(正如您所做的那样: <applicationName value="Lantic YCS WebServer" />, Lantic YCS WebServer是您的源)并在您的安装程序中创建此源(因为Setup-User应具有管理员权限) )。

Well this question was asked long time back, I have a suggestion which may be of some help to anyone looking for a solution to this issue. 这个问题很久以前被问到了,我有一个建议可能对寻找这个问题的解决方案的任何人有所帮助。 If you are converting console app to windows service... you must have created an installer for installing the app. 如果要将控制台应用程序转换为Windows服务...您必须已创建用于安装应用程序的安装程序。 When you install the app, it creates a installation folder for the service usually under ProgramFile..... (Check it when you are installing your service..it gives you the path to the installation directory). 当您安装应用程序时,它通常在ProgramFile下创建服务的安装文件夹.....(在安装服务时检查它..它为您提供安装目录的路径)。 If in your web.config file you have provided path to Log file as "Log\\Log.txt", then there will be a folder(Log) in the installation folder with your Log file(Log.txt). 如果在web.config文件中提供了日志文件的路径为“Log \\ Log.txt”,那么安装文件夹中将有一个文件夹(Log)和您的日志文件(Log.txt)。 You are wasting your time if you are looking for log file in your application folder. 如果您在应用程序文件夹中查找日志文件,则会浪费时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM