简体   繁体   中英

appending date in filename in C# app.config using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener

I am using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener in my app.config of C# application as below:

 <listeners>
  <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
       listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
       fileName="RollingFlatFile.log" 
       footer="----------------------------------" 
       formatter="Text Formatter" 
       header="" rollInterval="Day"
       timeStampPattern="yyyy-MM-dd hh:mm:ss"
       traceOutputOptions="DateTime, Timestamp" 
       filter="All" />
  <add name="Event Log Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" source="Application" formatter="Text Formatter" log="Application" machineName="." traceOutputOptions="None" />
</listeners>

I want that filename should contain date also like RollingFlatFile_19Nov2020.log . Can anyone help how I can achieve this only by doing changes in this configuraion file.

Solution: Set date in environment variable through code. That variable is accessible ro web.config or app.config. Below is the example-

C# code-

Environment.SetEnvironmentVariable("TODAYDATE", DateTime.Now.ToString("dd_MM_yyyy"), EnvironmentVariableTarget.Process);

And in app.config , we can use this variable like this-

 <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           fileName="Logs/RollingFlatFile_%TODAYDATE%.log"
           footer="----------------------------------"
           formatter="Text Formatter"
           header="" rollInterval="Day"
           timeStampPattern="yyyy-MM-dd hh:mm:ss"
           traceOutputOptions="DateTime, Timestamp"
           filter="All" />

it will create log file in folder Logs and file name will be RollingFlatFile_14_12_2020.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM