1
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, 
    log4net" />
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
    <system.serviceModel>
        <!-- Redacted -->
    </system.serviceModel>
  <log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <file value="logs\" />
      <datePattern value="'Proxy_'dd.MM.yyyy'.log'" />
      <staticLogFileName value="false" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <priority value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    <category name="Client.log">
      <priority value="ALL" />
    </category>
  </log4net>
  <applicationSettings>
    <!-- Redacted -->
  </applicationSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

我在安装 Windows 服务时有上面的配置,并且我在我正在安装的服务的 Progam.cs 文件中初始化我的记录器:

static void Main()
{
    XmlConfigurator.Configure();
    _logger.Debug("ProxyServerService Started.");
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    { 
        new ProxyServerService() 
    };
    ServiceBase.Run(ServicesToRun);
    _logger.Debug("ProxyServerService Terminated.");
}

当我在与该服务通信的应用程序中使用几乎相同的配置时,它会创建一个日志目录并将日志写入该目录。但是当我运行该服务时,什么也没有发生。

4

1 回答 1

2

原来我看不到日志的原因是因为我在活动目录中配置了日志记录器,即 Windows\system32 并且 Windows 不允许我这样做。

我更改了以下行:

<file value="logs\" />

至:

<file value="C:\AppName\logs\" />

它工作正常。

于 2013-07-05T11:14:15.223 回答