There are several possible ways to keep track of how your WebParts are doing. Since it is in most cases not possible to debug on a live system we write messages to logfiles. The tool we most often use in our WebParts is
log4net.
Log4Net is very flexible, but what if you want to use an interface to make it possible to switch the logging implementation in a later phase of your software? Without recompiling code?
Well, if you asked yourself this question you came to the right spot. :-)
The solution I’m going to show is to use
“Common Infrastructure Libraries for .NET”.
Common Logging and Log4Net is a great team. Because log4net is very fast and
supports many frameworks and common logging supports
many adapters.
But enough of the talk, you came here to see code and config right? ;-)
An example with a textfile and the eventlog as a logging-target.
Your settings in the web.config:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
<configSections>
...
<!-- START Defining the sections -->
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging, Version=1.2.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1B44E1D426115821" />
<!-- END Defining the sections -->
</configSections>
...
<!-- START configuration of common.logging to use log4net -->
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net, Version=1.2.0.2, Culture=neutral, PublicKeyToken=AF08829B84F0328E">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
<!-- END configuration of common.logging to use log4net -->
<!-- START configuration of log4net to use the eventlog and a rolling file -->
<log4net debug="true">
<!-- rolling file -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\Log4Net_Logs\\LogRoll.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- eventlog -->
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<threshold value="ERROR" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="An error ocurred in the MOSS application (please check the logfile for further information):%newline%newline %message" />
</layout>
</appender>
<!-- default logging config -->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="EventLogAppender" />
</root>
</log4net>
<!-- END configuration of log4net to use the eventlog and a rolling file -->
...
</configuration>
Make sure that the webserver has the right to write to the configured destination of the logfile.
The code:
...
using Common.Logging;
...
namespace namics.SharePoint.WebPart.AwesomeWebPart
{
public class CommonLog : Microsoft.SharePoint.WebPartPages.WebPart
{
ILog log = LogManager.GetLogger(typeof(AwesomeWebPart));
...
protected override void Render(HtmlTextWriter writer)
{
// Log an debug level message
if (log.IsDebugEnabled) log.Debug("begin render.");
...
}
...
}
}
Those of you who are already using log4net can see that the code with the common.logging looks like the log4net code.
Of course there are many other possible combinations of frameworks and tools. This is just one of them. And in many cases using log4net directly instead of using an interface is good enough.