2

This is first time I am using Log4net. It doesn't show any errors, but is not writing to file. I added following in my AppConfig file:

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

 <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline"/>
      </layout>
    </appender>

    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\CE\FileControllerLog.txt"/>
      <layout type="log4net.Layout.PatternLayout">
        <!--<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>-->
        <param name="ConversionPattern" value="%d %m%n"/>
      </layout>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <maxSizeRollBackups value="2"/>
      <maximumFileSize value="10MB"/>
      <staticLogFileName value="true"/>
    </appender>
    <logger name="FileControllerLog">
      <level value="INFO"/>
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender"/>
    </logger>
[assembly: log4net.Config.XmlConfigurator(Watch = true)] //added in assembly info file

and in code:

ILog logger = log4net.LogManager.GetLogger("FileControllerLog");
logger.Info("Method starts");

It's not logging.

4

3 回答 3

5

尝试这个:

 <log4net>
    <root>
      <level value="Error" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="<FilePath>" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="14" />
      <countDirection value="1" />
      <maximumFileSize value="4096KB" />
      <lockingModel type="log4net.Appender.FileAppender.MinimalLock" />
      <staticLogFileName value="true" />
      <PreserveLogFileNameExtension value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newlineDate[%date] ThreadID[%thread] Level[%-5level] Logger[%logger]] - %message%newline" />
      </layout>
    </appender>
  </log4net>

除此之外,配置部分应该有一个 Log4Net 条目,示例:

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

在您的 Logger 类中有ILog变量,并在构造函数中配置记录器,如下所示:

private ILog log;

public Log4NetLogger()
{
    log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    log4net.Config.XmlConfigurator.Configure();
}
于 2013-07-26T07:09:05.297 回答
1

可能是用户权限问题。

Windows 服务通常以没有写入权限的 NetworkService 凭据开始。

您可以为 C:\CE 文件夹添加写入和 midify 权限或使用另一个 Appender(如 EventLog Appender)

祝你好运

于 2013-07-26T08:43:24.050 回答
0

确保您运行服务的凭据下的用户对您尝试保存日志的位置具有写入权限

于 2013-07-26T07:10:52.000 回答