Month: July 2017

Log4net: Including stack trace into the logs

The log4net version included in Sitecore doesn’t contains the stack trace. That means that you cannot use %stacktrace or %stacktracedetail in the conversionsPattern.

I have found the way of how to do it. As a workaround, you should override the appender!

First create the following class:

public class SitecoreLogFileWithStackTraceAppender : SitecoreLogFileAppender
{
 protected override void Append(LoggingEvent loggingEvent)
 {
 var msg = loggingEvent.MessageObject as string;
 if (msg != null)
 {
 var prop = loggingEvent.GetType().GetField("m_data", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
 var mData = (LoggingEventData)prop.GetValue(loggingEvent);
 mData.Message = $"{msg}{Environment.NewLine}{Environment.StackTrace}";
 prop.SetValue(loggingEvent, mData);
 }
base.Append(loggingEvent);
 }
}

Second step, register newly created log4net appender:

<appender name="StackFileAppender" type="AndreyVinda.Loggers.SitecoreLogFileWithStackTraceAppender, AndreyVinda.Loggers">
 <file value="$(dataFolder)/logs/log.Stacktraces.{date}.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5t %d{yyyy-MM-dd HH:mm:ss.fff} %-5p %c - %m%n%n" />
</layout>
</appender>

For one of my projects I have chosen to log all the errors and all the messages that contains [SD] within the stacktrace. The config file is the following:

<appender name="StackFileAppender" type="AndreyVinda.Loggers.SitecoreLogFileWithStackTraceAppender, AndreyVinda.Loggers">
 <file value="$(dataFolder)/logs/log.Stacktraces.{date}.txt" />
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="[SD]"></stringToMatch>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5t %d{yyyy-MM-dd HH:mm:ss.fff} %-5p %c - %m%n%n" />
</layout>
</appender>

Enjoy!

Advertisements

Sitecore: Force of Query String Parameters

Sitecore is a web based system and just like to others systems it uses query string parameters to pass viable data. They are used to explicitly configure such important things as display modes, sites, languages etc. Remembering of those tricky things could have a huge speed improvement in cases when dealing with multi-language, multi-device or page editor enabled solutions. Below is the list of the Sitecore query string parameters that I have been using in my day-to-day job.

Continue reading “Sitecore: Force of Query String Parameters”