7

I am trying to modify the output name that is defined by appender in "Sitecore.config"

<file value="$(dataFolder)/logs/log.{date}.txt"/> specifically {date}

Every time I start a new instance of Sitecore, during the same day it creates a new file with different processID.

example: log.20161214.txt log.20161214.123456.txt log.20161214.484151.txt log.20161214.789914.txt

I would like to have all the logs for that one day to be in one "log.yyyDDmm.txt".

Is there a way to modify a log4net output? Or maybe there is another solution?

I am using Sitecore 8.1

  • Are you sure in your examples? SitecoreLogFileAppender at the and of the file adds formatted DateTime.Now.ToString("HHmmss"), not processID. ProcessID will be set only if you add to file {processid}. See methods GetTimedFileName and OpenFile of SitecoreLogFileAppender – Vlad Shpak Dec 15 '16 at 9:37
  • It adds some kind of information to the logs after the date. I am only specifying {date}. – Tony Dec 15 '16 at 15:58
  • 1
    ` <appender name="AuditLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging"> <file value="$(dataFolder)/logs/audit/audit.log.{date}.txt" /> <appendToFile value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" /> </layout> <encoding value="utf-8" /> </appender> ` – Tony Dec 15 '16 at 15:59
13

You can use a RollingFileAppender instead of the default Sitecore one and configure it to roll by date.

You can find samples in the log4net config examples section of the documentation.

Specifically with Sitecore, change the appender(s) to the following:

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender, Sitecore.Logging">
  <file value="$(dataFolder)/logs/log" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <maxSizeRollBackups value="30" />
  <datePattern value=".yyyyMMdd'.txt'" />
  <staticLogFileName value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
  </layout>
  <encoding value="utf-8" />
</appender>
  • file : The name of the file to log to. Note that we supplement this using below settings.
  • rollingStyle : The format to roll each file on. This causes a new file to be generated per day.
  • datePattern : The suffix to add to the file each time it is rolled. Note that the file extension is included here in single quotes. See this previous answer for more details.
  • staticLogFileName : If set to true then the latest log file will always have the same name, but note that due to your file value there is no file suffix so the would just be called log with the above settings.

The files will now be generated in format log.yyyMMdd.txt.

  • This solution just creates one instance of the file, if I restart SiteCore nothing is being added to the file file. Is there a reason for this? This is what I had originally: <appender name="AuditLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging"> <file value="$(dataFolder)/logs/audit/audit.log.{date}.txt" /> <appendToFile value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" /> </layout> <encoding value="utf-8" /> </appender> – Tony Dec 16 '16 at 15:46
  • Looks like you've added a new appender. Did you also register the logger in <root>? Alternatively, to replace the existing appender make sure the name attribute matches the existing one. – jammykam Dec 16 '16 at 17:48
2

You can use the log4net.Appender.FileAppender

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="log-file.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

For Minimal locking with FileAppender you can use something like below

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="${TMP}\log-file.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

And as a suggestion, RollingFileAppender works better for most scenarios. If your requirement is to have unified view of all log files for a particular category you can try Sitecore Log Analyzer module from marketplace

And to know more on what other appenders are available check this link from log4net

1

Seems someone has covered this, and shows where sitecore adds the process id https://sitecore.unic.com/2015/01/27/create-a-single-sitecore-log-file-per-day

However worth considering why sitecore adds the process id to the log file if it already exists http://hectorcorrea.com/blog/log4net-thread-safe-but-not-process-safe/17 When IIS recycles with overlapped recycling (default), there could be two processes trying to write to the same file, and then the second process won't log anything. So maybe it's not a bad idea to have the process id in the file name, as more important that logging works than in same file.

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.