我在IIS 8服务器上安装了.NET 4.5.2 Web API 2应用程序,它使用log4net(1.2.13.0)进行日志记录。我也在使用温莎城堡。问题是log4net日志记录在生产服务器上工作了好几天甚至几周。但是,由于未知原因,它会停止向日志文件写入任何内容。我的log4net配置如下:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="TextFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\MyApplication.log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %property{Guid} - %message%n" />
</layout>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="toaddress@domain.com" />
<from value="noreply@domain.com" />
<subject value="MyApplication production environment - ERROR" />
<smtpHost value="my.smtp.server.com" />
<bufferSize value="1" />
<lossy value="false" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR" />
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<acceptOnMatch value="true" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %property{Guid} - %message%n" />
</layout>
</appender>
<root>
<level value="All"></level>
<appender-ref ref="TextFileAppender" />
<appender-ref ref="SmtpAppender"/>
</root>
</log4net>
</configuration>
我首先尝试为IIS帐户的Logs文件夹设置写入权限 - 没有任何影响。我首先怀疑这是因为这是客户的服务器,并且某些管理员可能正在搞乱文件夹权限。然后我使用以下设置设置log4net内部调试:
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
<appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
它写入log4net.txt文件,一切似乎都是有序的。内部日志中的一些示例:
log4net: Searched for existing files in [C:\Logs]
log4net: curSizeRollBackups starts at [0]
log4net: [.2016-05-26] vs. [.2016-05-30]
log4net: Initial roll over to [C:\Logs\MyApplication.log.2016-05-26]
log4net: Moving [C:\Logs\MyApplication.log] -> [C:\Logs\MyApplication.log.2016-05-26]
log4net: curSizeRollBackups after rollOver at [0]
log4net: Opening file for writing [C:\Logs\MyApplication.log] append [True]
log4net: Created Appender [TextFileAppender]
log4net: Adding appender named [TextFileAppender] to logger [root].
log4net: Loading Appender [SmtpAppender] type: [log4net.Appender.SmtpAppender]
正如您所看到的,未写入日志时有4天的差距。但现在奇怪的是。现在,日志记录也成功写入C:\ Logs \ MyApplication.log文件,即使我删除了log4net内部调试的设置。所以似乎设置log4net内部调试就解决了问题。但这不是适当的长期解决方案。
任何想法在这里发生了什么?