Log4Net Rolling File Appender 滚动问题

发布于 2024-08-15 19:35:07 字数 1085 浏览 15 评论 0原文

我有以下 log4net 配置:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="Logs/%date{yyyy-MM-dd} Service.log" />           
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <rollingStyle value="Date"/>
    <datePattern value="yyyy-MM-dd"/>
    <maxSizeRollBackups value="100"/>
    <maximumFileSize value="15MB"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger: %message%newline" />
    </layout>
</appender>

数据将不断记录到日志文件中并滚动正常,但我最终得到如下滚动文件:

2009-12-21 Service.log2009-12-22    (this is what it will write tonight)  
2009-12-21 Service.log    <-- this being the latest file  
2009-12-21 Service.log2009-12-21    <-- last updated 23:59

我希望文件如下所示:

2009-12-21 Service.log
2009-12-22 Service.log
2009-12-23 Service.log

I have the following log4net config:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="Logs/%date{yyyy-MM-dd} Service.log" />           
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <rollingStyle value="Date"/>
    <datePattern value="yyyy-MM-dd"/>
    <maxSizeRollBackups value="100"/>
    <maximumFileSize value="15MB"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger: %message%newline" />
    </layout>
</appender>

Data will be logged constantly to the log file and rolls OK but I end up having rolled files like this:

2009-12-21 Service.log2009-12-22    (this is what it will write tonight)  
2009-12-21 Service.log    <-- this being the latest file  
2009-12-21 Service.log2009-12-21    <-- last updated 23:59

I want the files to be like:

2009-12-21 Service.log
2009-12-22 Service.log
2009-12-23 Service.log

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

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

发布评论

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

评论(3

柳絮泡泡 2024-08-22 19:35:07

删除文件元素中的文件名和类型:

然后将 datePattern 更改为(注意:确保转义“Service”中的字母' 适当地,就像 'log' 中的 g 是一种特殊格式,所以你需要用 '\' 对其进行转义):

<datePattern value="yyyy-MM-dd Service.lo\g"/>

Get rid of the file name and the type in the file element:

<file value="Logs\" />

Then change your datePattern to (note: make sure you escape the letters in 'Service' appropriately, like the g in 'log' is a special format, so you need to escape it with the '\'):

<datePattern value="yyyy-MM-dd Service.lo\g"/>
黑寡妇 2024-08-22 19:35:07

我认为以下内容应该是您所需要的。

中添加以下元素。

<staticLogFileName value="true" />

I think the following should be what you need.

Add in the following element within your <appender />.

<staticLogFileName value="true" />
三岁铭 2024-08-22 19:35:07

您可以使用下面的功能。
在此函数中,首先获取您在 webconfig 中设置的文件位置,然后您可以添加任何您想要的路径! (例如日期、我们的客户或...)

WebConfig:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\t4\\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="_yyyy-MM-dd.lo'g'"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date User:%identity IP:%X{addr} Browser: %X{browser} Url: %X{url} [%thread] %-5level %c:%m%n"/>
      </layout>
</appender>

功能:

public static void ChangeFileLocation(string _CustomerName,string _Project)
{
    XmlConfigurator.Configure();
    log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();          

    foreach (IAppender a in h.Root.Appenders)
    {
        if (a is FileAppender)
        {
            FileAppender fa = (FileAppender)a;
            string sNowDate=  DateTime.Now.ToLongDateString();
            // Programmatically set this to the desired location here
            string FileLocationinWebConfig = fa.File;
            string logFileLocation = FileLocationinWebConfig + _Project + "\\" + _CustomerName + "\\" + sNowDate + ".log";

            fa.File = logFileLocation;
            fa.ActivateOptions();
            break;
        }
    }
}

结果如下:C:\Logs\TestProject\Customer1\Saturday, August 31, 2013.log

You can use the function below.
In this function first get file location that you set in webconfig and after that you can add any path that you want! (like Date, our Customer, or...)

WebConfig:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\t4\\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="_yyyy-MM-dd.lo'g'"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date User:%identity IP:%X{addr} Browser: %X{browser} Url: %X{url} [%thread] %-5level %c:%m%n"/>
      </layout>
</appender>

Function:

public static void ChangeFileLocation(string _CustomerName,string _Project)
{
    XmlConfigurator.Configure();
    log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();          

    foreach (IAppender a in h.Root.Appenders)
    {
        if (a is FileAppender)
        {
            FileAppender fa = (FileAppender)a;
            string sNowDate=  DateTime.Now.ToLongDateString();
            // Programmatically set this to the desired location here
            string FileLocationinWebConfig = fa.File;
            string logFileLocation = FileLocationinWebConfig + _Project + "\\" + _CustomerName + "\\" + sNowDate + ".log";

            fa.File = logFileLocation;
            fa.ActivateOptions();
            break;
        }
    }
}

and result like this: C:\Logs\TestProject\Customer1\Saturday, August 31, 2013.log

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