开发者

Log4Net Rolling File Appender Rolling issue

开发者 https://www.devze.com 2022-12-14 09:52 出处:网络
I have the following log4net config: &开发者_运维百科lt;appender name=\"RollingFileAppender\" type=\"log4net.Appender.RollingFileAppender\">

I have the following log4net config:

&开发者_运维百科lt;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


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


I think the following should be what you need.

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

<staticLogFileName value="true" />


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

0

精彩评论

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

关注公众号