One of the things that I could never quite figure out about the SharePoint API is that they expose just about everything under the sun but when it comes to using their diagnostics / logging framework, it's all hands off. Granted, if you have the MOSS version, you can use PortalLog.LogString() method to write entries to the SharePoint ULS but for me that wasn't good enough. I wanted more control, control similar to what SharePoint does with their applications. Basically, I wanted the ability to set thresholds for my own diagnostics categories so that I could send anything that I wanted to the logs and depending on the threshold (verbose vs. unexpected) the system would determine whether to write it to the ULS and/or Windows Event Viewer.
Interesting enough, there isn't much information out there on how to do this so I had to start from scratch with the help of this article and Reflector. After much research, I think I am getting closer (if not almost done) to providing a way to log to SharePoint logs (and as an added bonus to the Event Viewer).
At a high level, there are four different aspects:
- DiagnosticsManager - In charge of performing the following:
- Adding new Categories to the Diagnostics Logging page (Operations in Central Admin)
- Controlling the thresholds (Event Severity & Trace Severity) for the new categories
- Returning the values of the thresholds (severity levels) for the requested category — used on the Diagnostics Logging page and when your application needs to know whether to log it or not.
- DiagnosticsLevel - Basically just a data structure class to hold information about the category defined above such as Category Name, Id, Event Severity and Trace Severity
- TraceProvider – This is the basically the class that you create when you following the MSDN article mentioned above (http://msdn.microsoft.com/en-us/library/aa979522.aspx).
- Logging (static class) – Static methods that you, the developer, would be calling when you want to log to the SharePoint ULS or Event Viewer. In this class, we check to make sure that the WriteEntry request that you want to log falls within the threshold of the category that you define. Example would be something like this…
SPLogger.RegisterApplication(LogCategory.MyCategory, "My SharePoint App", "Provosioning new Site", 1091);
SPLogger.WriteEntry(TraceSeverity.Verbose, "Stupid error message goes here");
SPLogger.WriteEntry(TraceSeverity.Unexpected, EventSeverity.Error, "Unexpected");
SPLogger.UnRegisterApplication();
Sample output:
03/12/2009 17:53:22.11 My SharePoint App (0×0CC8) 0×0B10 My Category Provosioning new Site 1091 Verbose Stupid error message goes here
03/12/2009 17:53:22.11 My SharePoint App (0×0CC8) 0×0B10 My Category Provosioning new Site 1091 Unexpected Unexpected error
This post is completely a high level overview because there isn't much sense going into details if nobody, aside from this insanely SharePoint obsessed developer, thinks it is useful
If you want more details, leave a comment and I'll follow up with additional posts.
UPDATE: I posted the wrong link for the MSDN article. AuditEntries are quite the same but I've my share of them too!
Eli
Yes I’m interested — I’ve seen lots of “Dangerous” implementations of ULS logging – I’d like to see it done correctly.