DanCode DanCode - 1 year ago 183
C# Question

Log4net: append TimeStamp in the name of a created file

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"?>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,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" />
<level value="ALL" />
<appender-ref ref="LogFileAppender" />

ivg ivg
Answer Source
<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"/>

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download