PowerPivotGeek?

Who is this mystery man?
Click on the icon to find out. Who is powerpivotgeek?

Howto: Write to the ULS

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.

  • Share/Bookmark

1 comment to Howto: Write to the ULS

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>