6

Is it possible to retrieve records from ULS log using C#/SharePoint API without having to manually parse log files? I would like to display logged messages on a custom web page, and I would like to filter these messages beforehand (based on area, category etc.). Ideally, it should be as simple as binding log records to a grid where Area=someValue.

There is plenty written about how to write to ULS log (using SPDiagnosticsServiceBase), but I can't find anything on how to read it. In SP2013, there is SPULSRetriever, but nothing similar seems to exist in SP2010.

UPDATE As suggested by the answer, here's one way to do it by invoking powershell.

using (PowerShell ps = PowerShell.Create())
{                
    ps.AddScript("Add-PSSnapin \"Microsoft.SharePoint.PowerShell\"");

    // example: call "Get-SPLogEvent -StartTime (Get-Date).AddHours(-24) | Where-Object {$_.Area -eq 'SharePoint Foundation'}" to get log records of last 24 hours
    string script = String.Format("Get-SPLogEvent -StartTime (Get-Date).AddHours(-24)");
    ps.AddScript(script);
    ps.AddCommand("where-object");
    ps.AddParameter("filterscript", ScriptBlock.Create("$_.Area -eq 'SharePoint Foundation'"));                             
    Collection<PSObject> output = ps.Invoke();

    foreach (PSObject outputItem in output)
    {
        if (outputItem != null)
        {                    
            // Display logged messages.
            Console.WriteLine(outputItem.Members["Message"].Value);                     
        }
    }
}
4

One quick way would be to have the C# program call PowerShell cmdlets to read the ULS log files and then use the results to display on a custom web page. The SharePoint PowerShell cmdlets (e.g., Get-SPLogEvent & Get-SPLogLevel) would provide the 'filtering' as needed -

Executing PowerShell scripts from C#

How to run PowerShell scripts from C#

Using Windows Powershell Cmdlets with ULS Logging

Logging and events cmdlets (SharePoint 2010)

Using Get-SPLogEvent to retrieve logs based on correlation id

Get-SPLogEvent (PowerShell cmdlet)

Get-SPLogLevel (PowerShell cmdlet)

  • Also to improve Get-SPLogEvent performance, this is useful – w128 Jun 5 '14 at 10:55
  • Another thing I remembered that is worth pointing out is that Get-SPLogEvent only searches the local server, which may not be desirable if multi-server environment is used. – w128 Jun 5 '14 at 13:08
  • I haven't tried it (yet) but PowerShell remoting in PowerShell 2.0 has been enhanced further in PowerShell 3.0 with a lot of new remoting features. It may help in accessing log files in other servers in the farm. – Supriyo SB Chatterjee Jun 5 '14 at 13:29
  • 1
    Then there's the ConvertTo-Html cmdlet to make the html pages for results display - technet.microsoft.com/en-us/library/ee156817.aspx – Supriyo SB Chatterjee Jun 5 '14 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.