简体   繁体   English

Log4Net不写入输出文件

[英]Log4Net Not Writing to Output File

I'm having serious trouble getting log4net to work with a Windows service (multi-project solution). 我在使log4net与Windows服务(多项目解决方案)一起使用时遇到了严重的麻烦。

I first added reference to log4net.dll to the appropriate projects via NuGet. 我首先通过NuGet将对log4net.dll的引用添加到适当的项目中。 I then created a new Log4Net.config file in the root folder of the Windows service project. 然后,我在Windows服务项目的根文件夹中创建了一个新的Log4Net.config文件。 In the file's properties, I set Copy to Output Directory = Copy always . 在文件的属性中,我将“ Copy to Output Directory = Copy always设置Copy to Output Directory = Copy always Below is the config file: 下面是配置文件:

<?xml version="1.0"?>
<configuration>
    <log4net>
        <appender name="TestServiceLog" type="log4net.Appender.RollingFileAppender">
            <file value="C:\Temp\Test.log" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="100" />
            <maximumFileSize value="10MB" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="INFO" />
            <priority value="ALL" />
            <appender-ref ref="TestServiceLog" />
        </root>
    </log4net>
</configuration>

In my Windows service project, in the AssemblyInfo.cs file, I added this line: 在我的Windows服务项目中,在AssemblyInfo.cs文件中,添加了以下行:

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

In my main TestService.cs class, I referenced log4net and initialized the logger like so (omitted non-logging code): 在我的主要TestService.cs类中,我引用了log4net并初始化了记录器,如下所示(省略了非记录代码):

public partial class TestService : ServiceBase
{
    private ILog Log { get; set; }

    protected override void OnStart(string[] args)
    {
        Log = LogManager.GetLogger(this.GetType());
        Log.Info("Hear me log.");
    }
}

I then installed and ran the service. 然后,我安装并运行了该服务。 I validated the service is running normally, but no log file is created/written to. 我确认服务正常运行,但是没有创建/写入日志文件。 No exceptions are being thrown. 没有异常被抛出。 Everything appears in order when debugging. 调试时一切都会按顺序出现。 I turned on log4net's internal debugging and got this: 我打开了log4net的内部调试,并得到了以下信息:

log4net: Creating repository for assembly [TestService, Version=3.6.5570.17497, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [TestService, Version=3.6.5570.17497, Culture=neutral, PublicKeyToken=null] Loaded From [C:\ProgramData\Company\Applications\TestService\TestService\TestService.exe]
log4net: Assembly [TestService, Version=3.6.5570.17497, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [TestService, Version=3.6.5570.17497, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: repository [log4net-default-repository] already exists, using repository type [log4net.Repository.Hierarchy.Hierarchy]

The output does not seem to hint at any issues. 输出似乎没有暗示任何问题。 I've also tried adding the following line to my configuration, but it didn't do the trick: 我还尝试将以下行添加到我的配置中,但没有成功:

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

I added a WinForms project to the solution with the exact same configuration and logging worked fine, so I'm confident it's not my setup. 我将完全相同的配置添加到解决方案的WinForms项目,并且日志记录工作正常,因此我相信这不是我的设置。 Any other ideas on steps I may have missed? 关于我可能错过的步骤还有其他想法吗?

I was able to get it to work by making the Log variable static and instantiating it like this: 我可以通过将Log变量设置为静态并实例化它来使其工作:

private static ILog Log = LogManager.GetLogger(typeof(TestService));

I'm not quite sure why this was necessary. 我不太确定为什么这是必要的。 Hopefully someone else knows the answer. 希望其他人知道答案。

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

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