Write to a Custom Sitecore Log with log4net
Sitecore developers know that it comes with its own built-in logging mechanism. This is actually based on log4net, a .NET port of log4j. Because Sitecore uses log4net for these logs, developers are able to implement additional logs via the log4net configuration. This post is an overview of how you can make your own custom log4net log for use in Sitecore. The idea of how to actually do it is quite simple, and as usual, a great one from John West.
John’s idea to implement something like this was to base the code off of the WebDAV custom log that Sitecore has. After de-compiling Sitecore.Diagnosics.WebDAV, I discovered you just need to get the Logger by its name. The name will just need to be defined in the web.config.
Configure the Logger and Appender
In the web.config, you need to define both a logger, and an appender. The logger will choose which appender to use. Under the <log4net> section of the web.config, add these two custom sets of nodes:
<appender name="CustomLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
<file value="$(dataFolder)/logs/custom.log.{date}.txt"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
</layout>
</appender>
<logger name="Sitecore.Diagnostics.Custom" additivity="false">
<level value="INFO"/>
<appender-ref ref="CustomLogFileAppender"/>
</logger>
Pay attention to the format of the log files in the appender. You can define how the file names are defined.
Code to Write to a Custom Log
Now that you have the log configured, it’s time to write some simple C# to write to the log.
var logger = log4net.LogManager.GetLogger("Sitecore.Diagnostics.Custom");
logger.Info("Hello, world. And by world, I mean a text file.");
The above code uses log4net’s API to get the new logger by its name. Once you have a logger, you can run any of the normal logging methods, such as Info(), Error(), Warn(), etc.
Do you have any thoughts or best practices when dealing with logs for Sitecore? If so, share your thoughts in the comments.
12 Comments + Add Comment
Got anything to say? Go ahead and leave a comment!
Popular Posts
- Using the DataSource Field with Sitecore Sublayouts
- Tame Your Sitecore Treelists
- Write to a Custom Sitecore Log with log4net
- Sitecore Upgrade Strategy
- Rendering Fully Qualified Sitecore URLs
- Sitecore Admin Pages Explained
- Managing CSS in the Sitecore Media Library
- Use Any() Instead of Count() To See if an IEnumerable Has Any Objects
- Sitecore Front-End Development Best Practices
- Scaling Sitecore Presentation Component Data Sources
Recent Comments
- Performance tuning your Sitecore installation | Agile and ALM: Software development today on A Going Live Checklist for Sitecore Websites
- Imran Saleem on Sitecore Avanced Database Crawler Occasionally Provides Null Results
- Ty Cahill on Sitecore Front-End Development Best Practices
- Sitecore Managed Sites as Virtual Folders | Fire Breaks Ice on Sitecore Item and Field Names
- Krimos on Using the DataSource Field with Sitecore Sublayouts
Sitecore Links
- .Sitecore
- Aboo Bolaky
- Alex Shyba
- Anders Dreyer
- aweber1.0
- Brian Pedersen
- Christopher Wojciech
- Coffee => Coder => Code
- Dev Sitecored²
- Everything Web
- Image0.com blog
- John West
- Learn Sitecore
- Let's do Sitecore
- Mark van Aalst
- Matthew Kenny
- Molten Core
- Project Lifecycle
- Sean Kearney
- Sebastian Patten
- Sitecore Australia
- Sitecore Blog
- Sitecore Climber
- Sitecore Development
- Sitecore Gadgets
- Techphoria414
- The Client View
- The Sitecore Experience
- Web Content Management and Delivery
Archives
- April 2013 (1)
- February 2013 (1)
- January 2013 (1)
- December 2012 (1)
- June 2012 (2)
- May 2012 (2)
- March 2012 (1)
- February 2012 (1)
- January 2012 (5)
- December 2011 (4)
- November 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (2)
- March 2011 (6)
- February 2011 (2)
- January 2011 (10)

Posted under:
ATM I have implemented Alex Shyba’s solution of sending all Sitecore logs to a table “Logs” in a separate DB. I am thinking of using your idea and extend it to write my own messages to a different table. Makes sense?
By the way, I just noticed that you posted this just two days ago! What a perfect timing!!!
Sounds like a great idea. For reference, Benjamin Vangansewinkel posted an additional blog post as a response to my forum post about this. In his post he show how you can have a RegEx to know to append to a custom log vs. the default Sitecore log. It’s worth a read:
Write you messages in a separated file. And here’s the original forum post.
So maybe you could implement a RegEx in your custom code that will write to custom table when the RegEx passes. Just a thought… but either way, you have a solid idea.
You can also log to your own file by passing in the logger to Sitecore Log method.
var logger = log4net.LogManager.GetLogger(“MyNamespace.Project.Custom”);
Sitecore.Diagnostics.Log.Debug(“Log this message in my own appender”, logger);
Or you can just pass in the “this” keyword and change the logger name to your project namespace that you calling from. I prefer the above with my own wrapper around it so I don’t have to keep requesting a logger at log time.
I’ve used you code and the custom log file is created however it is blank.
Any ideas why this would be?
I’ve got the same issue that the custom file is blank. ???
Same problem: log file is empty. I’ve called log4net.Config.XmlConfigurator.Configure(); as well just to be sure but still nothing in the log file.
I have created a custom log file successfully and its working fine. But I have to access it from directory, where as I want to show it to the Sitecore user from Log Viewer of Sitecore only. How to show my custom log files in the list of log viewer?
Thanks Mark , the code works as a separate log file. But i am not able to display that file in the list of log files that appear in SiteCore shell. Can you help?
I’m having the same problem as Ben, Hasham and Joe. The log file is always blank. I’ve tried it in a brand new Sitecore instance and it works, but not within an existing (upgraded to) 6.5 solution. Any thoughts Mark?
Got the same blank log. My problem was that i included the new “fresh” log4net dll file. Use the Sitecore.Logging one and i was good to go. Hope it helps someone.
[...] wie die Standard-Sitecore-Logging-Konfiguration verbessert werden kann. Zum Beispiel kann mit einem eigenen log4net Appender und logger ein eigenes Log-File als Ausgabemedium genutzt [...]
Thanks Mark, it works great!