Online Training On SharePoint

Tuesday, 20 April 2010

Writing to ULS Logs using PowerShell in SharePoint 2010

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.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), 
                                                    "Writing to the ULS log:  {0}",
                                                    new object[] { "My Custom Message!"});           

No comments:

Related Posts with Thumbnails