Empty Sitecore Custom Log Files?

This will be my shortest blog post till now, I will share a tip to take care of when you want to configure custom log for your sitecore solution. There are a lot of articles and Stack Overflow questions that tell you how to configure a custom log and I followed a few of nice reads,

Write you messages in a separated file

Write to a Custom Sitecore Log with log4net

The problem I faced was even after following the steps the log files were empty and no logging was happening. I reviewed thrice what I configured and also got it double checked with few of my colleagues at Horizontal Integration everything was in place still no logs written to my custom file.

This is what I configured,

  1. Adding Appender in web.config file
  2. <appender name="CustomLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
          <file value="$(dataFolder)/logs/SFTPLogs/SFTP.log.{date}.txt" />
          <appendToFile value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
          </layout>
        </appender>
    
    
  3. Adding logger in web.config file
  4. <logger name=" MyLogs.Logging.CustomLogger" additivity="false">
          <level value="INFO" />
          <encoding value="utf-8" />
          <appender-ref ref="CustomLogFileAppender" />
        </logger>
    
    
    
  5. A logger class
  6. 
    using log4net;
    namespace MyLogs.Logging
    {
        public static class CustomLogger
        {
            private static ILog log;
            public static ILog Log
            {
                get
                {
                    return log ?? (log = LogManager.GetLogger(typeof(CustomLogger)));
                }
            }
        }
    
    
  7. Finally calling the logger

namespace MyProject.Controllers
{
    public class SearchController : GlassController
    {
public ActionResult SiteSearch(int? page)
        {
CustomLogger.Log.Info("CustomLogger called from searchcontroller");
}
}
}

Everything configured perfectly, still no entries in the SFTP log files and the problem was with the reference of the log4net.dll in the custom logger class.

ILog was coming from log4net, so a developer’s natural tendency would be to add reference of log4net.dll into the project which was the cause of logging not working and having empty log files. Thanks to Michael from Sitecore Support to point me out in correct direction and asking me to add reference of Sitecore.Logging.dll instead of log4net.dll directly.

Note: Logger class is defined in a separate Class Library project and not in Web Application project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: