C#Log4net每日日志文件配置&用法示例

时间:2011-04-07 02:25:51

标签: c# log4net

有人可以提供一个如何设置&以cY中使用的log4net登录YYYYMMDD格式的每日日志文件。日志文件应为Data_20110407.txt,Data_20110408.txt,依此类推。

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

And in c# how should i call to log into this specific log? Log4Net.Error() or something?

感谢您的帮助!

2 个答案:

答案 0 :(得分:5)

以下是配置部分:

                  

    <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>

以下是您记录的方式:

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);
}

答案 1 :(得分:3)