问题
I have a Windows C# application which uses log4net for logging. This is how the loggers are configured:
<log4net>
<appender name="DebugFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Local\logs\ApplnTrace.log" />
<threshold value="INFO" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%5p] - (%file:%line) %m%n" />
</layout>
</appender>
<appender name="MSGFileAppender" type="log4net.Appender.RollingFileAppender">
<file value=".\local\logs\MsgTrace.log" />
<threshold value="INFO" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %n%m%n" />
</layout>
</appender>
<logger name="ApplnLogger">
<appender-ref ref="DebugFileAppender" />
</logger>
<logger name="MsgLogger">
<appender-ref ref="MSGFileAppender" />
</logger>
</log4net>
The files are rolled over based on date sometimes and sometimes does not and when files are not rolled over, the logging also stops. Can anybody help me find out why?
回答1:
log4Net is designed to fail quietly. When things stop working, there's usually a problem (most often, it's a config issue or a filesystem security issue).
You might want to try enabling internal log4net debugging. Park this key in the <appSettings>
element of your app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
then start your app. Log4net will spew a bunch of debugging info. It gets written to System.Console and to the System.Diagnostics.Trace system. You can catch the trace messages from an attached debugger or by adding a trace listener in the app.config file. If the trace listener writes to a file, make sure that your process has write access, otherwise you'll see nothing:
<configuration>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt"
/>
</listeners>
</trace>
</system.diagnostics>
...
</configuration>
There are a number of different trace listeners you can wire up here, including ones that write to the windows event system.
More here:
- http://haacked.com/archive/2006/09/27/Log4Net_Troubleshooting.aspx
- http://logging.apache.org/log4net/release/sdk/log4net.Util.LogLog.InternalDebugging.html
回答2:
Without source, it's hard to tell. Does the application also stop working? If yes, find the fatal flaw in your application.
If not, judging from the log4net JIRA, there's plenty of issues with the RollingFileAppender
(see: log4net JIRA) :
"RollingFileAppender stops logging intermittently when it tries to roll over the log file"
"RollingFileAppender with rollingStyle="Date" and maximumFileSize both are not working Simultaneously."
Maybe it's one of those issues that's hurting you.
来源:https://stackoverflow.com/questions/4819765/log4net-does-not-roll-over-the-log-file-sometimes-when-rollingstyle-set-to-date