Tag Archives: ULS Viewer

SharePoint Powershell | Get Detailed Information about Correlation IDs in ULS

I use this often enough I thought I’d write a quick post about it.  Basically, whenever an issue occurs on SharePoint we get a nice friendly error message with a correlation ID, which is great for troubleshooting the issues assuming that we have a nice timeframe for when the issue occurred.  We can quickly open the ULS Viewer, locate the correct log file, and filter for the correlation ID.   Unless of course we don’t have a time frame or we run a large farm, then it becomes more of a biblical “seek and ye shall find” approach.

That is where the beauty of PowerShell comes in.  There is a great command called Get-SPLogEvent which allows us to access the ULS logs through PowerShell and returns results from a Unified Logging Service (ULS) trace log.

Now, this is a great tool in general for being able to write monitoring scripts for SharePoint by using the following:

Get-SPLogEvent -MinimumLevel "Error"

Use the StartTime and EndTime flags and you can basically monitor your system every couple of minutes and if an Error level item is returned you can send yourself an email using the Send-MailMessage command.

However, what inspired this post is that I yet again received an e-mail with a screenshot of a friendly SharePoint error page telling me “An unexpected error has occurred.” (Do we every have an expected error?).  So, logging into the server and firing up my handy PowerShell windows I can use the following to find the specific Correlation ID in the ULS to quickly diagnose the issue.

Bring up the SharePoint 2010 Management shell on your server and enter the following:

Get-SPLogEvent | ?{$_.Correlation -eq "Correlation ID of Error"} | ft Category, Message -Autosize

Substituting the “Correlation ID of Error” string for the GUID in your screenshot.

If you want more detail than just the Category  and Message, just select the additional fields:

Get-SPLogEvent | ?{$_.Correlation -eq "Correlation ID of Error"} | select Area, Category, Level, EventID, Message | Format-List

and if you want to pipe it into a file so you can email it off to your SharePoint support group (because sometimes in large organizations there is more than just one person responsible for the SharePoint farm) you can use this to create a file:

Get-SPLogEvent | ?{$_.Correlation -eq "Correlation ID of Error"} | select Area, Category, Level, EventID, Message | Format-List > c:\SPCorrelationErr.log

And just like that, easy ways to extract the information you are looking for to help troubleshoot your SharePoint farm.

Enable CollectSPRequestAllocationCallStacks with PowerShell

Every once in a while the ULS viewer will throw this error message:

An SPRequest object was reclaimed by the garbage collector instead of being explicitly freed.  To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.  Allocation Id: {057C0995-CC5A-4652-B5A3-48B46A225269}  To determine where this object was allocated, set Microsoft.SharePoint.Administration.SPWebService.ContentService.CollectSPRequestAllocationCallStacks = true.

The following Powershell script will enable the CollectSPRequestAllocationCallStacks:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadFile($Env:CommonProgramFiles+"\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll") | out-null

# Get Content Service of the farm
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

# Display and change the setting of property "CollectSPRequestAllocationCallStacks"
write-host "Current: " $contentService.CollectSPRequestAllocationCallStacks
$contentService.CollectSPRequestAllocationCallStacks = $true

write-host " New: " $contentService.CollectSPRequestAllocationCallStacks