As more and more folks start to get into PowerPivot and SharePoint, there is a need to programmatically write entries into the ULS. Since all of PowerPivot and all of SharePoint share this common logging infrastructure, why not also include log entries from your own processes. Here is some sample code for doing that:
First via PowerShell:
$diagSvc = [Microsoft.SharePoint.Administration.SPDiagnosticsServices]::Local
$category = new-object Microsoft.SharePoint.Administration.SPDiagnosticsCategory(“My Category”,
[Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable,
[Microsoft.SharePoint.Administration.EventSeverity]::Error )
$diagSvc.WriteTrace(0, $category, [Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable, “Write your log here” )
Now via C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
namespace WriteIntoULSLog
{
public class Program
{
public static void Main(string[] args)
{
SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteTrace( 0,
new SPDiagnosticsCategory(“My category”, TraceSeverity.Monitorable, EventSeverity.Error),
TraceSeverity.Monitorable,
“Writing to the ULS log: {0}”,
new object[] { “SharePoint 2010 rocks!”});
}
}
}
Enjoy.


[...] Continue reading Howto: Write to the ULS [...]