0

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
2
  • 2
    I don't follow.. what do you want to prevent? Commented Dec 21, 2009 at 23:32
  • Mauricio I want to prevent it from writing the log files like this 2009-12-21 Service.log2009-12-21 it should be 2009-12-21 Service.log 2009-12-22 Service.log 2009-12-23 Service.log
    – user236483
    Commented Dec 22, 2009 at 1:04

3 Answers 3

1

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

I think the following should be what you need.

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

<staticLogFileName value="true" />
1
  • Sorry, i still get the same problem
    – Gaven
    Commented Dec 22, 2009 at 4:17
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.