How to store Sitecore Logs on the ApplicationLog

There are several ways to store Sitecore Logs, depending on your specific scenario, one would fit better than an other.

Generally if your servers are monitored with products such SCOM or NewRelic that keep track of the application logs and allow you to define events and retention policy on the logs it make sense to write Sitecore Logs on the application log.

Configuring Sitecore to write the logs on the Application logs is pretty simple, what you need is to define a new Log4Net appender that point to  log4net.Appender.EventLogAppender, Sitecore.Logging

       <appender name =" SitecoreEventLogAppender" type="log4net.Appender.EventLogAppender, Sitecore.Logging"   xdt:Transform= "Insert " >
         <param name =" ApplicationName" value="SitecoreCD" />
         <layout type =" log4net.Layout.PatternLayout">
              <conversionPattern value =" %4t %d{ABSOLUTE} %-5p %m%n"/>
       <root xdt:Locator =" Condition(/configuration/log4net/root)" xdt:Transform="Replace" >
         <priority value =" INFO" />
         <appender-ref ref =" SitecoreEventLogAppender"  />

Having said that there are some security issues to create for the first time an entry on the Application Log (for your application name) and you can either follow the Sitecore KB or you can decide to create the first entry on the application log with a console application running it as Administrator…. In the case you would go for the second option, don’t forget to change the Application Name to the value that you need….

Here you can download a simple Console Application to create the first log on the application Log.

Sitecore Logging – Log4Net – CastleWindsor

As most of you probably already know, Sitecore (until 6.6.0) use an old version of Log4Net in the Sitecore.Logging assembly, you can read more about this issue here

So there are 2 options:

1) Use Sitecore Logging as your logging strategy

2) Use CastleWindsor Logging facilities

In order to implement the second option you have to:

Implement your logging facility:

public class  XXXContainerInstaller : IWindsorInstaller
public void Install(Castle.Windsor.IWindsorContainer container,
Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)

//Load your Custom Configuration from a different config “using a more advanced pattern expression

container.AddFacility&lt;Castle.Facilities.Logging.LoggingFacility&gt;(f =&gt; f.LogUsing(LoggerImplementation.Log4net).WithConfig(“log4net.xml”));




Prepare your config:

<facility id=”logging” type=”Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging” loggingApi=”log4net”/>
<component id=”loggingSetup” service=”XXX.ILoggingSetup, AssemblyName” type=”XXXX.Log4NetLoggingSetup, Asos.Framework” lifestyle=”AssemblyName”/>
<component id=”loggingContext” service=”XXX.ILoggingContext, AssemblyName” type=”XXXX.Log4NetLoggingContext, AssemblyName” lifestyle=”singleton”/>

The Log4Net Section will still be required and shared between your Castle Logging and SiteCore Logging…

<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net”/>

<appender name=”eventLogAppender” type=”log4net.Appender.EventLogAppender, log4net” logName=”Application”>
<param name=”LogName” value=”Application”/>
<param name=”ApplicationName” value=”XXXXX”/>
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%appdomain [%thread] %-5level – %message%newline [%properties]%newline %exception”/>
<level value=”ERROR”/>
<appender-ref ref=”eventLogAppender”/>

Be also aware that the SitecoreLogging does not support the latest log4net formatting syntax like %exception

and Sitecore formatting expression prefer the OldLog4Net syntax with all abbreviated letters

<conversionPattern value=”%4t %d{ABSOLUTE} %-5p %m%n” />

More references about this issue can be found: