Logging is an important way to ensure that the people responsible for maintaining a Sitecore system are able to identify and fix problems.
Sitecore uses Apache log4net for logging. Configuration settings for log4net are defined in Web.config under configuration/log4net.
Do not put log4net settings in a Sitecore patch file. Log4net is not a Sitecore component. Only Sitecore components can be configured using Sitecore patch files.
By default Sitecore uses the following logs:
Each log has a log level assigned to it. This log level limits the messages that are written to the log. Log level is also called priority.
For example, on a testing server you may be looking for any opportunity to tune performance. In this case, you would lower the log level so you get as much information from the system as possible. But on a production server, you might only be interested in errors. By setting the log level appropriately on each server, you have the ability to capture the amount of logging needed.
The following is a list of log levels (priorities) from lowest to highest severity.
This means that if the log level is set to INFO, messages logged at the INFO level will be recorded, as will messages logged at all of the higher levels (WARN, ERROR, and FATAL). DEBUG messages will not be logged because they have a lower level than INFO.
The following is an excerpt from Web.config that shows the log level (priority) assigned to the main Sitecore log:
<log4net>
<appender name="LogFileAppender"
type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
<file value="$(dataFolder)/logs/log.{date}.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
</layout>
</appender>
<root>
<priority value="INFO" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
In your integration you may want to write to the Sitecore log. The class Sitecore.Diagnostics.Log contains a number of methods that provide logging at different severity levels.
AuditAn audit message indicates that a user has performed an action in the Sitecore client. Audit messages are INFO level messages. They will only be recorded if the level level is set to INFO.
The following code…
var msg = "my audit message";
Log.Audit(msg, this);
… results in the following message in the log:
3936 11:52:40 INFO AUDIT (sitecore\admin): my audit message
DebugA debug message records information that may be useful during troubleshooting or debugging, but is generally not needed on a routine basis. As a result, most Sitecore servers are not set to record debug messages.
The following code…
var msg = "my debug message";
Log.Debug(msg, this);
… results in the following message in the log:
3940 15:35:54 DEBUG my debug message
ErrorAn error message indicates an error has occurred.
The following code…
var msg = "my error message";
Log.Error(msg, this);
… results in the following message in the log:
3936 11:52:40 ERROR my error message
FatalA fatal message indicates something has happened where significant loss of functionality is likely to follow.
The following code…
var msg = "my fatal message";
Log.Fatal(msg, this);
… results in the following message in the log:
3936 11:52:40 FATAL my fatal message
InfoAn info message indicates a routine condition. INFO is the default logging level on a Sitecore server, so any Sitecore server that has not been changed will include info messages in its log.
The following code…
var msg = "my info message";
Log.Info(msg, this);
… results in the following message in the log:
3936 11:52:40 INFO my info message
SingleErrorThis method is used to record error conditions that should only be recorded in the log one time.
The following code…
var msg = "my single error message";
Log.SingleError(msg, this);
… results in the following message in the log:
3940 15:35:54 ERROR my single error message
SingleFatalThis method is used to record fatal conditions that should only be recorded in the log once time.
The following code…
var msg = "my single fatal message";
var ex = new Exception("My fatal exception);
Log.SingleFatal(msg, ex, this);
… results in the following message in the log:
3940 15:35:54 FATAL SINGLE MSG: my single fatal message
Exception: System.Exception
Message: My fatal exception
WarnA warn message indicates a condition that, while not an error, is something an administrator might want to investigate.
The following code…
var msg = "my warn message";
Log.Warn(msg, this);
… results in the following message in the log:
3940 15:35:54 WARN my warn message
Sitecore allows you to add custom logs.
The main Sitecore log is sufficient in many cases, but there are cases when custom logs are useful:
You should also be aware of the disadvantages of using custom logs:
Since Sitecore uses log4net, log messages can be written to a variety of sources: files, databases, event viewer, and more. This section describes how to add a custom log named MyLog that is written to a file.
Web.config configuration > log4netAdd the following:
<appender name="MyLogFileAppender"
type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
<file value="$(dataFolder)/logs/log.{date}.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
</layout>
</appender>
<logger name="MyLog" additivity="false">
<priority value="INFO" />
<appender-ref ref="MyLogFileAppender" />
</logger>
The following is an example of the code needed to write to a custom log:
var logger = Sitecore.Diagnostics.LoggerFactory.GetLogger("MyLog");
logger.Info("This is an info message for my custom log");
Consider creating a helper class for custom logs. When you write to the Sitecore log you use the
Sitecore.Diagnostics.Logclass. Creating a similar class for a custom log makes it easier to write to the log in a consistent way.