C# Log4net 每日日志文件配置和使用示例

发布于 2024-10-30 17:56:17 字数 351 浏览 3 评论 0原文

有人可以提供一个关于如何设置和安装的示例吗?以 YYYYMMDD 格式登录到 C# 中使用的 log4net 的每日日志文件。日志文件的名称应为 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?

感谢您的帮助!

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 call to log into this specific log? Log4Net.Error() or something?

Thanks for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-11-06 17:56:17

这是配置部分:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文