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:

[csharp]
<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>
[/csharp]

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.

[csharp]
var logger = log4net.LogManager.GetLogger("Sitecore.Diagnostics.Custom");
logger.Info("Hello, world. And by world, I mean a text file.");
[/csharp]

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.

 

Mark Ursino

Mark is Sr. Director at Rightpoint and a Sitecore MVP.

 

19 thoughts on “Write to a Custom Sitecore Log with log4net

  1. 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!!!

    1. 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.

  2. 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.

  3. I’ve used you code and the custom log file is created however it is blank.
    Any ideas why this would be?

  4. 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.

  5. 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?

  6. 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?

  7. 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?

  8. 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.

    1. This is important to note because it will result in empty log files getting written. I did the same thing and only upon debugging and inspecting the logger did I find that log4net was throwing an exception internally because the internal AppenderCollection existed in both log4net.dll and Sitecore.logging.dll

  9. Another way to do this

    var logger = Sitecore.Diagnostics.LoggerFactory.GetLogger(“Sitecore.Diagnostics.Custom”);
    logger.Info(“info message here”);

  10. Any one can please explain me which one is the best from log4net and Sitecore.Diagnostics.Log.Error() and why?

  11. How to get this working with log4net.LogManager not Sitecore.LogManager ?

    I have a library that DOES NOT reference Sitecore, but only uses log4net.

Leave a Reply to Mark Ursino Cancel reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.