log4net FileAppender故障排除

时间:2016-01-29 13:25:16

标签: c# wcf log4net log4net-configuration

我们已经使用log4net一段时间了,但偶尔会遇到记录器每天都无法翻转(通常是我们的服务)的问题。这会引起严重的麻烦,因为这意味着我们会在没有警告的情况下丢失日志记录,并且必须每天检查日志是否成功。

我终于启用了log4net调试,并且每当有人第一次尝试点击服务时,我们的WCF服务(处理所有DAL调用)一直都会收到此错误。

log4net: FileAppender: Opening file for writing 
[C:\xxx\xxx\xxx\xxx.20160129.txt] append [False]
log4net:ERROR [RollingFileAppender] Unable to acquire lock on file C:\xxx\xxx\xxx\xxx.20160129.txt. Access to the path 'C:\xxx\xxx\xxx\xxx.20160129.txt' is denied.
log4net:ERROR [RollingFileAppender] OpenFile(C:\xxx\xxx\xxx\xxx,False) call failed.
System.ArgumentException: Stream was not writable.
   at System.IO.StreamWriter..ctor(Stream stream, Encoding encoding, Int32 bufferSize, Boolean leaveOpen)
   at System.IO.StreamWriter..ctor(Stream stream, Encoding encoding)
   at log4net.Appender.FileAppender.OpenFile(String fileName, Boolean append)
   at log4net.Appender.RollingFileAppender.OpenFile(String fileName, Boolean append)
   at log4net.Appender.FileAppender.SafeOpenFile(String fileName, Boolean append)
log4net:ERROR [RollingFileAppender] Failed in DoAppend
System.ArgumentNullException: Value cannot be null.
Parameter name: writer
   at log4net.Layout.PatternLayout.Format(TextWriter writer, LoggingEvent loggingEvent)
   at log4net.Appender.AppenderSkeleton.RenderLoggingEvent(TextWriter writer, LoggingEvent loggingEvent)
   at log4net.Appender.TextWriterAppender.Append(LoggingEvent loggingEvent)
   at log4net.Appender.FileAppender.Append(LoggingEvent loggingEvent)
   at log4net.Appender.AppenderSkeleton.DoAppend(LoggingEvent loggingEvent)

我们的log4net配置如下。

<log4net debug="false">
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="[%d] [%-5p] [%logger]: %m%n------------------------------------------------------------------%n" />
            </layout>
        </appender>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="C:\xxx\xxx\xxx\xxx" />
      <param name="AppendToFile" value="true"/>
      <param name="RollingStyle" value="Date"/>
      <param name="DatePattern" value=".yyyyMMdd.\t\x\t"/>
      <param name="StaticLogFileName" value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="[%d] [%-5p] [%logger]: %m%n------------------------------------------------------------------%n"/>
      </layout>
    </appender>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="xxx@yyy.com"/>
      <from value="xxx@yyy.com"/>
      <subject value="Error Occurred in xxx Service"/>
      <smtpHost value="xxx.xxx.xxx"/>
      <bufferSize value="512"/>
      <lossy value="true"/>
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="Error"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline"/>
      </layout>
    </appender>
    <root>
      <!--ALL -> DEBUG -> INFO -> WARN -> ERROR -> FATAL -> OFF-->
      <level value="ALL"/>
      <appender-ref ref="LogFileAppender"/>
            <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="SmtpAppender"/>
    </root>
    <logger name="SmtpLog">
      <!--ALL -> DEBUG -> INFO -> WARN -> ERROR -> FATAL -> OFF-->
      <level value="ALL"/>
      <appender-ref ref="SmtpAppender"/>
    </logger>
  </log4net>

这个基本模式对于我们的Web应用程序(ASP.NET)和Winforms应用程序来说非常有效,但似乎每天都在失败。

如果我们重启服务,它会立即采摘记录。这是目标文件夹的权限问题吗?源文件夹?或完全不同的东西?

我还应该提一下,运行该服务的用户可以完全访问我们正在编写日志文件的目录。

1 个答案:

答案 0 :(得分:0)

这就是为什么你不相信顶级权限会一直向下。

事实证明我们使用的文件夹层次结构类似于以下内容: c:\GROUP_NAME\APPLICATION_NAME\SERVICE_FOLDERc:\GROUP_NAME\APPLICATION_NAME\WEB_APPLICATION_FOLDER

服务用户具有C:GROUP_NAMEAPPLICATION_NAME的访问权限,但关键是,不在SERVICE_FOLDER 。我已经更新了权限,以便在SERVICE_FOLDER级别包含服务用户的显式权限,我完全希望能够解决此问题。