With SharePoint 2010 the capability to write in ULS Logs has been significantly improved. We can now easily write our own custom message using SharePoint Object model or PowerShell:
Following PowerShell script can be used to write to ULS logs:
$diagSrc = [Microsoft.SharePoint.Administration.SPDiagnosticsServices]::Local
$diacategory = new-object Microsoft.SharePoint.Administration.SPDiagnosticsCategory(“My Category”,
[Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable,
[Microsoft.SharePoint.Administration.EventSeverity]::Error )
$diagSrc.WriteTrace(0, $diacategory, [Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable, “Write your log here” )
If we need to write using SharePoint Object Model following code can be used:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
namespace CustomMessageToULSLog
{
public class ULSLogMessage
{
public static void Main(string[] args)
{
SPDiagnosticsService diagSrc = SPDiagnosticsService.Local;
diagSrc.WriteTrace( 0,
new SPDiagnosticsCategory("Custom category", TraceSeverity.Monitorable, EventSeverity.Error),
TraceSeverity.Monitorable,
"Writing to the ULS log: {0}",
new object[] { "My Custom Message!"});
}
}
}