Friday, July 6, 2012

Custom log files for the Sitecore CMS

I realize that in most cases, the purpose of doing this would be to avoid the hassle of digging through tons of Sitecore log messages to find your own. In my case, I really wanted to separate the custom messages so as to avoid polluting the Sitecore logs. There's actually a lot of information out there on how to write your custom log messages into a separate log file, so I'm not going to go into that. There is an extensive post by John West on Logging with Sitecore, which can get you started on successfully separating log entries into a new file and to the point where I found myself. My custom messages were successfully written to a separate file, but they also continued to get appended to the regular Sitecore logs.

I had to do some digging to configure the regular logs to ignore my custom messages, so I'm writing this in the hopes that I might save someone else the digging.

I ended up adding a filter to the LogFileAppender that would deny messages containing my custom string. The message would be denied on match.

<appender name="LogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
  <file value="$(dataFolder)/logs/log.{date}.txt"/>
  <filter type="log4net.Filter.StringMatchFilter">
     <stringToMatch value="YOUR_CUSTOM_STRING" />
     <acceptOnMatch value="false" />
  <appendToFile value="true"/>
  <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>