1

I am using below configuration for log4net appender to log entries into Custom log file. The requirement is to have one log file for one day and all logs should append to this one file.

<appender name="CustomLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
      <file value="$(dataFolder)/Logs/Custom/CustomLog{date}.log"/>
      <encoding value="utf-8" />
      <staticLogFileName value="true"/>
      <datePattern value=".yyyyMMdd.'log'" />
      <rollingStyle value="Date" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d (%-5p) %n%m%n%n" />
      </layout>
    </appender>

Even, when the setting <appendToFile value="true" /> is true but still new file is creating with name of "CustomLog20180613.123945.log".

2

You should change your appender like this:

<appender name="CustomLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
  <file value="$(dataFolder)/Logs/Custom/CustomLog.log"/>
  <encoding value="utf-8" />
  <staticLogFileName value="false"/>
  <datePattern value="_yyyyMMdd" />
  <rollingStyle value="Date" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <appendToFile value="true" />
  <preserveLogFileNameExtension value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d (%-5p) %n%m%n%n" />
  </layout>
</appender>

This should generate correct log files with names like CustomLog_20180613.log

RollingStyle attribute set to Date will do all the magic here for you and it will put formatted date string based on datePattern before ".log" extension.

To make this work together preserveLogFileNameExtension attribute needs to be set to true to maintain ".log" extension in filename. Also staticLogFileName attribute needs to be set to false.

  • @Shailesh - Have you tried to change config like I suggested? If it worked as expected please upvote and accept this answer. – Peter Procházka Jun 18 '18 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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