Setting, Saving & Restoring Logging Level for SharePoint using PowerShell

Background Information:

We want to have a script that will automate the configuration of the SharePoint ULS log by configuring the diagnostic logging with PowerShell. Assume you have a problem in production, you may wish to change the logging level of all logging categories to verbose for a short period of time to allow you to capture as much information about the problem as possible. The downside of this approach however, is that increasing the amount of information captured obviously increases the disk space taken by the logs. It is therefore crucial for you to be able to easily bring the level of information captured down after the “monitoring” window is over. I am a strong believer that most of the time, setting your log levels to Error will be enough to give you a good indicator of where important issues are within your environments.

To achieve this, we will be creating a PowerShell script that will simply prompt the user to enter the name of a logging level, and will then go and set all logging categories in the SharePoint farm to that level. I believe this script is something every SharePoint admin should keep handy in their back pockets.

PowerShell Methods:

In order for us to be able to change the logging level of a SharePoint farm, PowerShell provides us with the Set-SPLogLevel. This PowerShell cmdlet accepts the –TraceSeverity parameter which specifies the logging level to set for the SharePoint farm. SharePoint also makes available the Clear-SPLogLevel PowerShell cmdlet to restore all logging categories to their default value (Information level). Using the Set-SPLogLevel cmdlet, the possible values for the trace severity level are:

  • None
  • ErrorCritical
  • Error
  • Warning
  • Information
  • Verbose

With this in mind, we can go ahead and create the following (extremely basic you’ll say) PowerShell script:

$level = Read-Host “Log Level (None, ErrorCritical, Error, Warning, Information, or Verbose)”

Set-SPLogLevel –Eventseverity $level

So these two lines of script will allow us to do just what we want, and will set the logging level for all the farm. But what if you had set special logging levels for specific categories? For example, your organization may have decided to set the User Profile Category to log all the “Verbose” information received, but left all other categories to “High”. How do you go about remembering these special configurations? Remember that the Clear-SPLogLevel simply puts all categories back to the “Information” level.

What we need is to add logic in our script so that it loops through all the current configuration, before changing it, saves it somewhere, and allow us to restore it later on. In order for us to be able to get a listing of what the current settings for the logging level, we need to make a call to the Get-SPLogLevel PowerShell cmdlet. Calling this cmdlet will return a full list of all logging categories along with their current logging level (see Figure below).

20140428-1.png

We will now be developing a new PowerShell function that will strictly be taking care of saving the information about the various logging categories and their associated logging level into a structured format that we could reuse later on to restore values to what they were. We will be saving the logging information in a text file under the C:\Temp\ folder. Make sure you modify this value to point to wherever you wish to have your file saved. The resulting text file will contain one line of text per logging category. Each line will start with the category’s area, the name of the category, the trace severity level, and the event severity level, all separated by a pipe character (|). The following PowerShell lines will declare such a function:

$newLine = “`r`n”

Function SaveCurrentLoggingSettings($path)

{             

                $content = “”

                $logSettings = Get-SPLogLevel

                foreach($setting in $logSettings)

                {

                                $content += $setting.Area.ToString() + “|” + $setting.Name.ToString() + “|” + $setting.TraceSev.ToString() + “|” + $setting.EventSev.ToString() + $newLine

                }

 

                if((Test-Path $path) -eq $false)

                {

                                New-Item $path -Type File -Force

                }

 

                $content | Out-File -FilePath $path

}

 

SaveCurrentLoggingSettings(“C:\temp\nik.txt”)

20140428-2.png

 

Now the second part is to write a function that will do the opposite, and read values out of that file and set the logging levels back to the values contained in it. The following PowerShell lines will read the content of the file, loop through each line one at a time, split them on the ‘|’ character, and then set their values back into your SharePoint Farm.

$newLine = “`r`n”

 

Function RestoreLoggingLevelsFromFile($path)

{

                $content = Get-Content $path

                $lines = $content.Split($newLine)

                foreach($line in $lines)

                {

                                if($line -ne “”)

                                {

                                                $segment = $line.Split(‘|’)

                                                Write-Host “Setting Levels on [“$segment[1]”] T=”$segment[2]”E=”$segment[3]

                                                Set-SPLogLevel -EventSeverity $segment[3] -TraceSeverity $segment[2] -Identity ($segment[0] + “:” + $segment[1])​

                                }

                }

}

RestoreLoggingLevelsFromFile(“C:\temp\nik.txt”)

20140428-3.png

Putting it All Together:

Now that we’ve managed to write a function to save the current logging configuration, and another to restore values from it, we can combine both and produce a single PowerShell script. For this tutorial, the script we will produce will do the following operations in sequence:

  1. Save the current Logging levels on disk at c:\Temp\nik.txt
  2. Set the logging level of all categories to “Error”
  3. Wait for the user to confirm a rollback
  4. Upon confirm rollback from user, will restore all values from file;

Here’s what the resulting script will look like:

$newLine = “`r`n”

 

Function RestoreLoggingLevelsFromFile($path)

{

                $content = Get-Content $path

                $lines = $content.Split($newLine)

                foreach($line in $lines)

                {

                                if($line -ne “”)

                                {

                                                $segment = $line.Split(‘|’)

                                                Write-Host “Setting Levels on [“$segment[1]”] T=”$segment[2]”E=”$segment[3]

                                                Set-SPLogLevel -EventSeverity $segment[3] -TraceSeverity $segment[2]

                                }

                }

}

Function SaveCurrentLoggingSettings($path)

{             

                $content = “”

                $logSettings = Get-SPLogLevel

                foreach($setting in $logSettings)

                {

                                $content += $setting.Area.ToString() + “|” + $setting.Name.ToString() + “|” + $setting.TraceSev.ToString() + “|” + $setting.EventSev.ToString() + $newLine

                }

 

                if((Test-Path $path) -eq $false)

                {

                                New-Item $path -Type File -Force

                }

 

                $content | Out-File -FilePath $path

}

 

SaveCurrentLoggingSettings(“C:\temp\nik.txt”)

Set-SPLogLevel –EventSeverity “Error”

Read-Host “Enter any key to restore configuration values”

RestoreLoggingLevelsFromFile(“C:\temp\nik.txt”)

20140428-4.png

Microsoft Premier Field Engineer - SharePoint

One thought on “Setting, Saving & Restoring Logging Level for SharePoint using PowerShell

  1. chandan

    Hi Nik,

    I ran the script for restoring it from the save settings but getting error and state is not changed to what it is in the backupfile

    Setting Levels on [ Connectors:Exchange ] T= Medium E= Information
    WARNING: The area or category name is not found: Connectors:Exchange
    Setting Levels on [ Connectors:File ] T= Medium E= Information
    WARNING: The area or category name is not found: Connectors:File
    Setting Levels on [ Connectors:Framework ] T= Medium E= Information
    WARNING: The area or category name is not found: Connectors:Framework
    Setting Levels on [ Connectors:HTTP ] T= Medium E= Information
    WARNING: The area or category name is not found: Connectors:HTTP
    Setting Levels on [ Connectors:Notes ] T= Medium E= Information
    WARNING: The area or category name is not found: Connectors:Notes
    Setting Levels on [ Connectors:People ] T= Medium E= Information
    WARNING: The area or category name is not found: Connectors:People
    Setting Levels on [ Connectors:SharePoint ] T= Medium E= Information
    WARNING: The area or category name is not found: Connectors:SharePoint
    Setting Levels on [ Content Index Server ] T= Medium E= Information
    Setting Levels on [ Crawler:Common ] T= Medium E= Information
    WARNING: The area or category name is not found: Crawler:Common
    Setting Levels on [ Crawler:Content Plugin ] T= Medium E= Information
    WARNING: The area or category name is not found: Crawler:Content Plugin
    Setting Levels on [ Crawler:FilterDaemon ] T= Medium E= Information
    WARNING: The area or category name is not found: Crawler:FilterDaemon
    Setting Levels on [ Crawler:Gatherer Plugin ] T= Medium E= Information
    WARNING: The area or category name is not found: Crawler:Gatherer Plugin
    Setting Levels on [ Crawler:Gatherer Service ] T= Medium E= Information
    WARNING: The area or category name is not found: Crawler:Gatherer Service

    Script used :

    Function RestoreLoggingLevelsFromFile($path)

    {

    $content = Get-Content $path

    $lines = $content.Split($newLine)

    foreach($line in $lines)

    {

    if($line -ne “”)

    {

    $segment = $line.Split(‘|’)

    Write-Host “Setting Levels on [“$segment[1]”] T=”$segment[2]”E=”$segment[3]

    Set-SPLogLevel -EventSeverity $segment[3] -TraceSeverity $segment[2] -Identity($segment[1])

    }

    }

    }

    RestoreLoggingLevelsFromFile(“D:\diagonsticbackup.txt”)

    }

    The above remains in verbose enabled state

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*