vrijdag 17 september 2010

SharePoint 2010 Logging to the ULS

Two days ago I started creating a small but effective logging component for SharePoint 2010. I made one for 2007 that could write to the ULS log (well got it mainly from my friend google) and I reaqd its much easier in the 2010 version of SharePoint. And it is, the older code could go out the door and the new way of handling logging only requires around 20 lines of code for a simple custom implementation.

I took a sample from somewhere that showed how to implement the DiagnosticsServiceBase, which was already having all the functionality I wanted. The code below is roughly the minimum you are required to write to get started.

[Guid("70B3CC24-E48E-4C02-9273-4BC6B6320AB7")]

public class MyDiagnosticsService : SPDiagnosticsServiceBase

{

public const string AreaName = "Virtual Affairs";

public MyDiagnosticsService()

{

}

public MyDiagnosticsService(string name, SPFarm parent)

: base(name, parent)

{

}

public static void WriteToLog(string category, string message, TraceSeverity? severity)

{

SPDiagnosticsCategory diagnosticsCategory = MyDiagnosticsService.Current.Areas[AreaName].Categories[category];

if (severity == null)

{

MyDiagnosticsService.Current.WriteTrace(0, diagnosticsCategory, diagnosticsCategory.DefaultTraceSeverity, message);

}

else

{

MyDiagnosticsService.Current.WriteTrace(0, diagnosticsCategory, (TraceSeverity)severity, message);

}

}

protected override IEnumerable ProvideAreas()

{

List categories = new List();

categories.Add(new SPDiagnosticsCategory(category, TraceSeverity.Unexpected, EventSeverity.Error));

List areas = new List

{

new SPDiagnosticsArea(AreaName, categories)

};

return areas;

}

}

Now I wanted to have my own categories, else I could just use the default DiagnosticsService, and have the Prduct column in the ULS populated with my product name. Now you get there by creating a similar class as shown above.

Now that is all working pretty well I wanted to have some standard categories in stead of having other developers just typing, or mistyping, their own. As the categories are predefined I don’t like to use string as they might be wrong. So I added a enum Category in the class so you can use one of those. I modified the WriteToLog so it takes a Category instead of a string and used the Enum.GetName() to get the string representation of the enum.

Now I was faced with the only thing I could just not get some decent info on and that was how I get it to show up in the Central Admin monitoring pages, specially the event throttling page. You do need to enable site administrators to set the level of detail they want to end up in the ULS log. Registering was mentioned and DiagnosticsService.Update() (in a powershell description on the msdn site). Also the mention of having to add a Registry key, which; a) I find not do professional in a delivery process and b) should be done on each web front in the farm (I guessed).

Funny enough I got it working in a farm level feature by calling MyDiagnosticsService.Update() and MyDiagnosticsService.Delete(). I had some troubles testing this, things got stuck and I only got twice the categories. But after I recreated a new SharePoint farm and added the solution I could turn on and of the Diagnostics area and categories. That is they appeared and disappeared when I expected them to.

So in a nutshell, to create custom logging to the ULS:

1. Implement a class based on DiagnosticsServiceBase

2. Use an enumeration to circumvent category naming typos and exceptions

3. Use the Update() and Delete() methods on your custom diagnostics class to register the area and categories.

Or, which I found out after browsing and searching, use the SharePoint Logger component which is part of a SharePoint Guidance toolkit. Found that too late and it was a bit overwhelming, have to evaluate that package when I have some free time to spend. Link is here:

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=64b55569-2168-4545-8b7c-f185b2cf967d&displaylang=en

If you have a bit of free time try to find it yourself. The download is SharePointGuidance2010. Try that name or SharePoint Guidance, I found it by sheer luck I guess.

And some other links I based my work on are:

http://blog.mastykarz.nl/logging-uls-sharepoint-2010/

http://spg.codeplex.com/ (annoying one cause it made me think I could download the code there)

http://msdn.microsoft.com/en-us/library/ff798371.aspx (fun and games from msdn, tells you all about it except where you get it => annoying aswell)

Geen opmerkingen:

Een reactie posten