namics SharePoint Weblog
Mit Windows SharePoint Services (WSS v3) und Microsoft Office SharePoint Server 2007 (MOSS 2007) zur professionellen eCollaboration Plattform
namics @ www.flickr.com

LINKS

  • namics Weblog
  • about:namics
  • namics Website

AKTUELLE ARTIKEL

  • MOSS 2007 auf SQL 2008 installieren
  • Sharepoint an der worldwide Partner Conference von Microsoft, 7. – 10. Juli 2008
  • Team Mika certified - MCTS Welcome Package
  • Aus gegebenem Anlass...
  • SharePoint Infrastructure Updates - Nachtrag
  • SharePoint Infrastructure Updates - Installation
  • SharePoint Infrastructure Updates
  • namics Fachtagung: Produktivitätssteigerung mit MOSS 2007
  • Windows Server 2008 Hyper-V RTM
  • MOSS, RichLinkSelector and some magic

KATEGORIEN

  • Business Intelligence
  • Document Management
  • Kollaboration
  • MOSS 2007
    • Berechtigungen
  • Microsoft Community
  • Office 2007
  • Silverlight
  • Tools und Applikationen
  • Virtualization
  • Visual Studio
  • Web Content Management
  • Windows Live
  • Windows Server 2008
  • Windows SharePoint Services v3
  • Workflow

ARCHIVE

  • August 2008
  • Juli 2008
  • Juni 2008
  • Mai 2008
  • April 2008
  • Februar 2008
  • November 2007
  • Oktober 2007
  • September 2007
  • Juni 2007
  • Mai 2007
  • April 2007
  • März 2007
  • Februar 2007
  • Januar 2007
  • Dezember 2006
  • November 2006
  • Oktober 2006
  • September 2006
  • August 2006

XML UND MUMBO JUMBO

  • Subscribe with Bloglines
  • Add to My Yahoo!
  • Add to Google
  • Atom Feed
  • RSS 2.0 Feed
  • Creative Commons License
    Dieses Weblog untersteht der Creative Commons Lizenz.
  • Powered by Movable Type 3.35
« Running Silverlight on Apache | Übersicht | Custom List Form WebPart erscheint nicht (Lösung) »
16
Mai
Logging in WebParts
gepostet von Reto Seiz am 16.05.2008 um 15:00
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.

TRACKBACK

TrackBack URL for this entry:
http://blog.namics.com/mt/mt-tb.cgi/1126

KOMMENTAR SCHREIBEN

Name:

E-Mail Adresse:

URL:

Bitte das Ergebnis von 1 + 2 als Ziffer (Spamschutz):