开发者

Log4net issue with RollingFileAppender

Following is the code:

  <log4net debug="lse">
    <!-- Define some output appenders -->
    <appender name="trace" type="log4net.Appender.TraceAppender, log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n"/>
      </layout>
    </appender>
    <!-- Add StrcuturedFN logger-->
    <appender name="StructuredFNLoggerAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\Log.txt"/>
      <param na开发者_运维知识库me="AppendToFile" value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="10MB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd hh:mm:ss:ff} - %m%n"/>
      </layout>
    </appender>

    <!-- Setup the root category, add the appenders and set the default priority -->
    <root>
      <priority value="WARN"/>
      <appender-ref ref="StructuredFNLoggerAppender"/>
    </root>

    <logger name="StructuredFNLogger.Logging">
      <level value="ALL"/>
      <appender-ref ref="StructuredFNLoggerAppender"/>
    </logger>

  </log4net>

The logger is logging but new file is not created when the size of the file grows more then 10MB.


Maybe this can help; Here is section of a working app config that is using RollingFileAppender.

<log4net>
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <param name="File" value="child" />
            <param name="CountDirection" value="1" />
            <param name="MaximumFileSize" value="10MB" />
            <param name="MaxSizeRollBackups" value="-1" />
            <param name="DatePattern" value=".yyyy-MM-dd" />
            <layout type="log4net.Layout.PatternLayout">
                <param name="Header" value="DateTime    Thread  Level   Logger  Message%newline" />
                <param name="ConversionPattern" value="%d   %t  %-5p    %c  %m%n" />
            </layout>
        </appender>
        <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
            <param name="File" value="delivery.log" />
            <param name="AppendToFile" value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <param name="Header" value="DateTime    Thread  Level   Logger  Message%newline" />
                <param name="ConversionPattern" value="%d   %t  %-5p    %c  %m%n" />
            </layout>
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
            </layout>
        </appender>
        <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
            <param name="Threshold" value="ERROR" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="[%thread] %-5level %logger%newline%message" />
            </layout>
        </appender>     
        <!--
        <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender" >
            <param name="Threshold" value="FATAL" />
            <param name="SmtpHost" value="-fill in-" />
            <param name="Subject" value="FUSION RIS Delivery Service " />
            <param name="To" value="-fill in-" />
            <param name="From" value="-fill in-" />
            <param name="Lossy" value="false" />
            <param name="BufferSize" value="1" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%newline%d [%thread] %-5level %logger%newline%message" />
            </layout>
        </appender>     
        -->
        <root>
            <level value="WARN" />
            <appender-ref ref="ConsoleAppender" />
            <appender-ref ref="RollingFileAppender" />
            <appender-ref ref="EventLogAppender" />
            <!--<appender-ref ref="LogFileAppender" />-->
            <!--<appender-ref ref="SmtpAppender" />-->
        </root>
    </log4net>


Turn on internal debugging and see if there is anything going on that shouldn't.

from the log4net FAQ:

There are 2 different ways to enable internal debugging in log4net. These are listed below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

The preferred method is to specify the log4net.Internal.Debug option in the application's config file.

Internal debugging can also be enabled by setting a value in the application's configuration file (not the log4net configuration file, unless the log4net config data is embedded in the application's config file). The log4net.Internal.Debug application setting must be set to the value true. For example: This setting is read immediately on startup an will cause all internal debugging messages to be emitted.

To enable log4net's internal debug programmatically you need to set the log4net.Util.LogLog.InternalDebugging property to true. Obviously the sooner this is set the more debug will be produced. Internal debugging messages are written to the console and to the System.Diagnostics.Trace system. If the application does not have a console the messages logged there will be lost. Note that an application can redirect the console stream by setting the System.Console.Out. The Trace system will by default send the message to an attached debugger (where the messages will appear in the output window). If the process does not have a debugger attached then the messages are sent to the system debugger. A utility like DebugView from http://www.sysinternals.com may be used to capture these messages.

As log4net internal debug messages are written to the System.Diagnostics.Trace system it is possible to redirect those messages to a local file. You can define a trace listener by adding the following to your application's .config file:

<configuration>
    ...

    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener"

                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>

    ... </configuration> 

Make sure that the process running your application has permission to write to this file.


Here is a working example of a rolling file appender:

<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="../Logs/Web-Log.xml"/>
        <appendToFile value="true"/>
        <rollingStyle value="Size"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="10MB"/>
        <layout type="log4net.Layout.XmlLayout">
            <param name="Prefix" value=""/>
        </layout>
    </appender>

    <root>
        <level value="ALL"/>
        <appender-ref ref="FileAppender"/>
    </root>
</log4net>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜