3

below is the code of my logger config file. When a new txt file is created in my LogFolder, my goal is to append the current timestamp in the name of the file. For example, on July 20th 2015 at 10:27:19am, the file name should be named "logger_2015-07-20_10-27-19.txt"

With the code that I provided, a logger file do get created, but its name is "logger-.txt". The timestamp failed to show up.

Do I need to provide a reference for "${byTimeStamp(local)}? If so, how would I be able to do that?

    <?xml version="1.0" encoding="utf-8"?>
        <configuration>
          <configSections>
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
          </configSections>
          <log4net>
            <timestamp key="byTimeStamp" datePattern="yyyy-MM-dd_HH-mm-ss" timeReference="contextBirth"/>
//Here is where I set the name of the logger txt.
            <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
              <param name="File" value="C:\LogFolder\logger_${byTimeStamp(local)}.txt" />
              <param name="AppendToFile" value="true"/>
              <param name="RollingStyle" value="Once"/>
              <param name="DatePattern" value="yyyy-MM-dd'.txt'" />
              <preserveLogFileNameExtension value="true"/>
              <maxSizeRollBackups value="20" />
              <staticLogFileName value="false" />
              <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
              </layout>
            </appender>
            <root>
              <level value="ALL" />
              <appender-ref ref="LogFileAppender" />
            </root>
          </log4net>
        </configuration>
DanCode
  • 525
  • 3
  • 7
  • 25
  • Related post - [Log4net rolling daily filename with date in the file name](https://stackoverflow.com/q/1165084/465053) – RBT Jul 15 '21 at 04:13

3 Answers3

9

Another way to accomplish this:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString">
        <conversionPattern value="log (%date{yyyy.MM.dd - HH-mm-ss}).log" />
    </file>
    [...]
</appender>
Torben Kohlmeier
  • 6,713
  • 1
  • 15
  • 15
  • upvote, but it actually breaks the "maxSizeRollBackups" - files are never deleted, because for the current pattern only one file is found. – Patrick Stalph Nov 22 '17 at 12:39
2
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
  <param name="File" value="c:\\ProjectX\\Log\\logger_.txt"/>
  <param name="AppendToFile" value="true"/>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <param name="RollingStyle" value="Date"/>
  <param name="DatePattern" value="yyyy.MM.dd"/>
  <param name="StaticLogFileName" value="true"/>
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
  </layout>
</appender>

As per Nuno G on Append current Date to Log file with Log4Net

Community
  • 1
  • 1
ivg
  • 439
  • 4
  • 7
  • 1
    My log file need to roll every time the application is run. I would need to create a new log file for every session. Having would not roll the log for each session. – DanCode Jul 20 '15 at 17:23
1

Requires a bit of code, and the secret is to call the configuration after updating the property so it gets picked up.

The config...

 <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="C:\logfolder\logger-%property{ts}.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1024KB" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %.30logger - %message%newline" />
      </layout>
    </appender>

Then in the code...

   var ts = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
   GlobalContext.Properties["ts"] = ts;
   log4net.Config.XmlConfigurator.Configure();

As mentioned above, every time you change the timestamp, you need to call the Configure() method so it gets picked up in the log name. This could be a bit ugly depending on how your logging code is written.

dbugger
  • 15,868
  • 9
  • 31
  • 33