Unified Logging Service (ULS) monitoring is a frequent event for administrators trying to keep their farms healthy. ULS logs key events in a series of text files on a variety of categories and can be throttled to verbose during periods of troubleshooting. The difficulty lies when trying to actually find errors in the log. Often, this is done simply by opening the log file in Notepad and using the find feature (CTRL-F) to find the “error” keyword, which usually indicates a problem needing further research. With thousands of lines of to sift through, this tedious process often consumes a long time.
Enter PowerShell, Microsoft’s Object-Oriented Scripting language. With PowerShell, SharePoint Administrators everywhere have a new way to quickly find ALL the errors in their SharePoint logs with one simple line of code. More information on PowerShell can be found here:
Windows PowerShell
http://technet.microsoft.com/en-us/library/bb978526.aspx
The solution that meets this need lies in the use of the select-string command. This command does one job: It Finds strings in files based on a pattern matching. Fortunately, select-string has a parameter “-Path” that allows you to search an entire folder full of files. So, finding the errors in the SharePoint logs is simply a matter of running the select-string command against a folder full of SharePoint ULS logs. Here is the syntax.
Select-string –Path “C:\Program Files\Common Files\Microsoft Shared\Web Servers Extensions\14\Logs\*.log” –Pattern “ error “ –SimpleMatch
Note the use of the *.log in the Path. (Yes, wildcards are allowed) In this case, Select-String will look for the word “ error ” in the logs. Yes, the spaces are important in the pattern match to avoid words like “Terrorism” from getting a hit in your search. If you run the command, however, you’ll find it can be hard to read. A simple solution to this problem is to export results to a file. The Export-CSV command can take results and export them to a file and path of your choosing. I recommend something simple that will also display at the top of your folder. (ie. _SharePointErrors.txt) Here is the command again, with the export feature added.
Select-string –Path “C:\Program Files\Common Files\Microsoft Shared\Web Servers Extensions\14\Logs\*.log” –Pattern “ error “ –SimpleMatch | Export-CSV –Path “C:\Error Logs\_SharePointErrors.txt”
This command takes the output from the pipeline and pipes the results to a Comma Separated (CSV) file. Using the –Append parameter will allow you to continually add more errors to the end of this file. There are many other parameters for these two, powerful commands. Follow these links for more information.
Select-String
http://technet.microsoft.com/en-us/library/hh849903.aspx
Export-Csv
http://technet.microsoft.com/en-us/library/hh849932.aspx
Piping and the Pipeline in Windows PowerShell
http://technet.microsoft.com/en-us/library/ee176927.aspx
Obviously this example is a basic solution that will allow any administrator to quickly find ULS errors in the farm and assist in a more proactive response to farm incidents. Enhancements may include storing the error files for all your farms in a common location, or dynamically generating the error log filename. Others may include importing this CSV data into a list or database.