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
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:
// 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.