开发者

C# Log4net Daily Log File Config & Usage Example

Could someone please provide an example on how to setup & log into a daily log file in YYYYMMDD format for log4net used in c#. The log file should be name Data_20110407.txt, Data_20110408.txt and so on.

<appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender">
bla bla...?

And in c# how should i cal开发者_如何学编程l to log into this specific log? Log4Net.Error() or something?

Thanks for help!


Here is the Config Section:

    <appender name="InfoRollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">

        <param name="File" value="auditlog"/>
        <param name="RollingStyle" value="Date"/>
        <param name="DatePattern" value="_yyyy-MM-dd.lo\g"/>
        <param name="StaticLogFileName" value="false"/>
        <maximumFileSize value="100KB" />
        <appendToFile value="true" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%newline ---------------START---------------------- %newline %date - %message %newline ----------------END--------------------- %newline" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <levelMin value="DEBUG" />
            <levelMax value="DEBUG" />
        </filter>
        <filter class="log4net.Filter.DenyAllFilter"/>
    </appender>

    <appender name="ErrorRollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">

        <param name="File" value="errorlog"/>
        <param name="RollingStyle" value="Date"/>
        <param name="DatePattern" value="_yyyy-MM-dd.lo\g"/>
        <param name="StaticLogFileName" value="false"/>
        <maximumFileSize value="100KB" />
        <appendToFile value="true" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%newline ---------------START---------------------- %newline %date - %message %stackTrace %newline ----------------END--------------------- %newline" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <levelMin value="WARN" />
            <levelMax value="ERROR" />
        </filter>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="InfoRollingLogFileAppender" />
        <appender-ref ref="ErrorRollingLogFileAppender" />
    </root>

</log4net>

Here is how you can log:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


public void LogError(string message)
{
    log.Error(Program.LogPrefix +"\r\r\nERROR:" + message);
}

public void LogAudit(string message)
{
    log.Debug(Program.LogPrefix + "\r\r\nINFO:" + message);
}


Please refer to Log4net rolling daily filename with date in the file name

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜