Sitecore for Developers
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,
- Adding Appender in web.config file
- Adding logger in web.config file
- A logger class
- Finally calling the logger
[code language="xml"]<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>
[/code]
[code language="xml"]<logger name=" MyLogs.Logging.CustomLogger" additivity="false"> <level value="INFO" /> <encoding value="utf-8" /> <appender-ref ref="CustomLogFileAppender" /> </logger>
[/code]
[code language="xml"]
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))); } } }
[/code]
[code language="xml"]
namespace MyProject.Controllers{ public class SearchController : GlassController {public ActionResult SiteSearch(int? page) {CustomLogger.Log.Info("CustomLogger called from searchcontroller");}}}
[/code]
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