Quick way to add an error or not to ULS logs

Here’s a quick neat snippet to add a comment to ULS logs. Helps to troubleshoot those hard errors.

using microsoft.sharepoint.administration;

//some code;
catch (Exception ex)
SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteTrace(0, new SPDiagnosticsCategory(“EMACS CUSTOM”, TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable,
“Writing to the ULS log: {0}”, new object[] { ex.ToString() });

Comments are closed.