Chaks' Corner

SharePoint and other stuffs

25. February 2010 21:21
by Chaks
1 Comments

SharePoint 2010: Log to ULS from code

25. February 2010 21:21 by Chaks | 1 Comments

Note: Only supported in SharePoint 2010 and higher versions

Something every SharePoint developer asks for and here it is how you log to ULS from your code in SharePoint 2010. Below is a console app:

logtouls_code

You need to add reference to Microsoft.SharePoint.dll. Very simple!

And here is our log from ULS Viewer:

llogtouls_ulsviewer

The code uses the following classes:

1) SPDiagnosticsCategory

2) SPDiagnosticsService

P.S: I will update when I find out how to add a Correlation ID associated with the log entry.

Comments (1) -

Alex Talarico

So I just started with this and the console app works like a charm.. however when I try to write in my App - I don't receive any errors, but it never writes!

SPDiagnosticsCategory spdiagcat = new SPDiagnosticsCategory("Hello ULS", TraceSeverity.Verbose, EventSeverity.Verbose);
            SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
            diagSvc.WriteTrace(100, spdiagcat, TraceSeverity.Monitorable, "Writing to the ULS log is not happening...", null);

Comments are closed