Logging in EPiServer
By default EPiServer ships with log4net for logging, and I posted this to remind myself what we usually put in our log4net configuration file. However, other logging tools can be used as well, Daniel Berg has mentioned EPiServer logging with ELMAH and Martin Söderlund has promoted NLog.
Goal of logging
For EPiServer websites we want to achieve the following with log4net:
- include everything emanating from our own business logic
- reduce clutter from log-intensive types
- split log files into one new log file per day
The contents of our EPiServerLog.config
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<!-- Use a new log file for each day -->
<appender name="fileLogAppender" type="log4net.Appender.RollingFileAppender" >
<file value="C:\Logs\website.com" />
<encoding value="utf-8" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<datePattern value=".yyyyMMdd.'log'" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level [%thread] %type.%method - %message%n" />
</layout>
</appender>
<!-- Log everything concerning site business logic (entire namespace) -->
<logger name="My.Website.Business">
<level value="All" />
</logger>
<!-- Log when EPiServer saves a page to the database -->
<logger name="EPiServer.DataAccess.PageSaveDB">
<level value="DEBUG" />
</logger>
<!-- Disable/reduce logging for log-intensive types -->
<logger name="EPiServer.Events.Remote.RemoteEventsManager">
<level value="Error" />
</logger>
<logger name="EPiServer.Web.InitializationModule">
<level value="Error" />
</logger>
<logger name="EPiServer.Framework.Initialization.InitializationEngine">
<level value="Error" />
</logger>
<!-- Levels: Off, Fatal, Error, Warn, Info, Debug, All -->
<root>
<level value="Warn" />
<appender-ref ref="fileLogAppender" />
</root>
</log4net>
One log file per day
The <file> element determines the folder and root filename of the log files. The <datePattern> element determines the date format appended to the root filename. The configuration above will result in log files being stored in the C:\Logs folder, and the logfiles will be called website.com.YYYYMMDD.log like this:
Logging specific namespaces
We’ve set the default logging level to Warn (see the <root> element), but we’ve defined different verbosity for different namespaces to make logging focus on types we’re particularly interested in.
For example, we reduce clutter from certain EPiServer types by setting the logging level to Error, but we enable verbose logging (using the Debug logging level) for the PageSaveDB class which handles page saves to the database.
We also log everything (using the All debug level) for all namespaces beneath the namespace My.Website.Business, since that’s our own logic. This includes namespaces below as well, for example My.Website.Business.PageTypes.
Troubleshoot log4net when nothing is logged
We’ve run into problems where nothing is logged by log4net. The next time we run into any problems like that we’ll make sure to:
- Verify there is no class-wide ILog field in the Global.asax class (that was quite a “gotcha” for me)
- Verify the application pool account has sufficient write permissions for the log folder
Here’s a relevant forum topic on EPiServer World if you run into problems with log4net in EPiServer.