![]() |
Enterprise Systems »
SharePoint Server »
General
Intermediate
License: The Code Project Open License (CPOL)
Implementation of Logging and Instrumentation Application Block in MOSS 2007By Madhur AhujaThe purpose of this article is to illustrate how we can effectively make use of the Application blocks within Sharepoint 2007 development tasks |
C#, .NET, Design
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
The purpose of this article is to illustrate how we can effectively make use of the Application blocks within Sharepoint 2007 development tasks.
If you have not read blog post about Enterprise Library and its application blocks, you can read it here, or otherwise at MSDN
For example, Data Access Application block can be effectively used when writing webparts to implement interaction of webpart with custom database, or even Caching Application Block to implement caching of data within the webpart.
One of the main portion of time while doing development is always devoted to implement effective use of Exception handling and logging.
A well designed application should always involve Exception handling and logging which involves:try catch blocks
catch filters to either recover out of a situation or display a friendly error message on screen
finally blocks to release unmanaged memory objects
Exceptions generated by user interface elements like webparts and controls can be simply displayed on the screen, however, exceptions generated by services like MOSS timer jobs and workflows can be quite tricky to handle.
They need to be logged full exception details and stack trace so that user can find out the cause of the exception.
Lets start on with configuring the Logging Application block to incorporate standard logging in MOSS 2007 ULS logs. The ULS logs are by default stored in Logs folder under 12 hive.
We will create a simple MOSS 2007 application to demonstrate simplest logging to sharepoint ULS logs using Enterprise Library.
Before proceeding a quick grasp of design of Logging Application Block is necessary, which can be found here
We will see how easy it has become to implement these functionalities with core functionalities being contained in wrapper classes of Enterprise library.
Lets start on with writing a custom Trace Listener, which will trace all our logs to sharepoint logs. Fire up VS 2005 and create a Class Library project.
We will create a custom class which implements the interface Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener
The interface requires implementation of three methods: TraceData, Write, WriteLine. The code for the same is shown below.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;
namespace Logging
{
[Configuration.ConfigurationElementType(typeof(Configuration.CustomTraceListenerData))]
public class SharepointTraceListener:TraceListeners.CustomTraceListener
{
string assb=System.Reflection.Assembly.GetCallingAssembly().FullName;
int level=TraceProvider.TraceSeverity.High;
string app="Sample App";
public override void TraceData(TraceEventCache eventCache, string
source,TraceEventType eventType, int id, object data)
{
if (data is LogEntry && this.Formatter != null)
{
this.WriteLine(this.Formatter.Format(data as LogEntry));
}
else
{
this.WriteLine(data.ToString());
}
}
public override void Write(string message)
{
TraceProvider.RegisterTraceProvider();
TraceProvider.WriteTrace(0,level, Guid.Empty, assb,app,app, message);
TraceProvider.UnregisterTraceProvider();
}
public override void WriteLine(string message)
{
TraceProvider.RegisterTraceProvider();
TraceProvider.WriteTrace(0, level, Guid.Empty, assb,app,app, message);
TraceProvider.UnregisterTraceProvider();
}
}
}
If you see the code above, it is awfully simple. Making use of just one Class TraceProvider, which outputs the details of the trace to log. The implementation of this class is given by Microsoft in WSS 3 SDK here, so I am not going to repeat it here.
We can just include the same implementation in our project from this location
To use our custom trace listener, we need to create custom Application configuration file. This can be created from the Configuration Tool provided by Microsoft Enterprise Library. Fire up the configuration tool and choose New Application and Add a new Logging Application Block.
Right click the Trace Listeners section and choose New Custom Trace Listener, and select your custom Trace Listener. In our case its Logging.SharepointTraceListener. Save the file as App.config.
Now since everything is ready, we can make use of our custom app.config (along with Enterprise Libraries binaries obviously) in our MOSS development applications like webparts, timer jobs, Workflows etc.
The steps needed for the same would be:
App.config to the VS 2005 project
Microsoft.Practices.EnterpriseLibrary.Logging and Microsoft.Practices.EnterpriseLibrary.Common
LogEntry and Logger classes of Enterprise Library to output trace to Sharepoint ULS logs using the CustomTraceListener
Here is a sample sharepoint Console Application which I built to demonstrate this :
using System;
using Microsoft.SharePoint;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common;
namespace SPConsole
{
class Program
{
static void Main(string[] args)
{
try
{
using (SPSite site = new SPSite("http://madhur"))
{
//write normal code
}
}
catch (System.IO.FileNotFoundException ex)
{
LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = ex.Message;
logEntry.Categories.Add("Trace");
logEntry.Categories.Add("UI Events");
Logger.Write(logEntry); //The log will be written to sharepoint ULS logs
}
}
}
}
In the code above, if the sharepoint site as specified URL is not found, the exception is directly traced to sharepoint ULS logs. The key thing to note here is that the location where the trace output is logged is completely encapsulated in App.config.
<listeners>
<add
listenerdatatype=
"Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
traceoutputoptions="Timestamp"
type="Logging.SharepointTraceListener, Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Sharepoint Trace Listener" formatter="Text Formatter">
</add>
</listeners>
By just changing the highlighted line in our App.Config above, we can make the trace to be output to different trace listener, email, database, flat file, XML file and so on. This is the real power of Microsoft Enterprise Library.
The code for this sample project can be downloaded here: Download Logging.zip - 1.52 MB
Feel free to analyze it , use it, modify it...
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Feb 2008 Editor: |
Copyright 2008 by Madhur Ahuja Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |