Writing to Sitecore General Log

19Feb

Writing to Sitecore General Log

Sitecore CMS utilizes Apache log4net™ library for outputting log statements to a variety of output targets. log4net, in turn, is .NET port of ubiquitous Apache log4j framework. Thus, both are very similar in use.

For simplicity of this post, we’ll assume our log is the Sitecore general log where we’d like to write different messages depending on a situation, or so-called “log level”. The current log level and the log configuration settings are configured in <configuration><log4net>...</log4net></configuration> block of Web.config file.

The following levels (listed from lowest to highest priority) are supported:

  • DEBUG (Log.Debug)
  • INFO (Log.Info, Log.Audit)
  • WARN (Log.Warn, Log.SingleWarn)
  • ERROR (LogError, Log.SingleError)
  • FATAL (Log.Fatal, Log.SingleFatal)

In order to write to the Sitecore log, you need to engage one of the available level methods in Sitecore.Diagnostics.Log class following this general convention:

Sitecore.Diagnostics.Log.[LevelMethod](message, this);

For example:

// Output a debug message when the current log level is set to DEBUG.
// Since this is the lowest priority message, it will not be written
// to the log when the level is set to anything other than the DEBUG or ALL.
Sitecore.Diagnostics.Log.Debug("Some debug message", this);

// Write a message to the log indicative of an application error.
// This message will be written when the log level is set to ERROR
// or FATAL. It will not be written for lower priority levels
// (e.g. DEBUG, INFO, WARN)
Sitecore.Diagnostics.Log.Error("Some app error message", this);

// Write an error message to the log, but write it one time only. 
Sitecore.Diagnostics.Log.SingleError("Some app error that should be written once", this);

...

Hopefully, this gives you an idea on how to get started with Sitecore logging. There is much more to it that was intentionally omitted to keep the post simple. The official Apache log4net™ project page can be found here.

Dmitri

Director of Technology

I am Dmitri Moore, a Director of Technology at ColdData and a hands-on coder for almost 2 decades now. During this time, I’ve seen many frameworks going from being super popular to fairly forgotten. So far, PEAN is my favorite stack. Let’s see how long this one is gonna stick around. Feel free to ping me with your own prognosis or just say “hi”.

Leave a Reply

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