Filtering errors in sql error logs

Posted on 2014-11-05
Last Modified: 2014-11-28
Hello Guys,

As a part of our verification process after patching or reboot, we have to check SQL ERRORLOGS for any major errors(in all the servers where changes has been applied).
I'm using the following query of checking the errors:

EXEC xp_readerrorlog 0, 1, 'failure'

Open in new window

In place of 'failure', I use keywords like 'corrupt', 'failed', etc.
I just need suggesstion in this case as to what all things should I check related to database errors, service status, etc. If you could please tell me or suggest me some new kind of query that I can use to filter major error in the SQL errorlogs.

Also one more favor. Is it possible to access these logs from powershell? Since, we have to carry out this procedure on many servers, just to save time.

Thanks in advance
Question by:Abhinav Singh
  • 2
  • 2
  • 2
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 40425598
Rather than filter the log, grab it all and remove the uninteresting stuff, and look at the rest.

The code below is part of a stored proc I created based on examples from Redgate

 CREATE Table #ErrorLog2000
 (ErrorLog varchar(4000),ContinuationRow Int)
 INSERT INTO  #ErrorLog2000  -- Stores the output of sp_readerrorlog
 EXEC sp_readerrorlog
 /* The code below deletes the rows in the error log which are mostly
 the SQL startup messages written into the error log */
 DELETE FROM #ErrorLog2000
    AND ContinuationRow = 0)
 OR (ErrorLog LIKE '%Intel X86%')
 OR (ErrorLog LIKE '%Copyright %')
 OR (ErrorLog LIKE '%Microsoft %')
 OR (ErrorLog LIKE '%All rights reserved.%')
 OR (ErrorLog LIKE '%Server Process ID is %')
 OR (ErrorLog LIKE '%Logging SQL Server messages in file %')
 OR (ErrorLog LIKE '%Errorlog has been reinitialized%')
 OR (ErrorLog LIKE '%Starting up database %')
 OR (ErrorLog LIKE '%SQL Server Listening %')
 OR (ErrorLog LIKE '%SQL Server is ready %')
 OR (ErrorLog LIKE '%Clearing tempdb %')
 OR (ErrorLog LIKE '%Recovery %')
 OR (ErrorLog LIKE '%to execute extended stored procedure %')
 OR (ErrorLog LIKE '%Analysis of database %')
 OR (ErrorLog LIKE '%Edition%')
 OR LEN(ErrorLog) < 25

You might like to add a filter here to delete anything older than the last hour or two.

I believe you could run this from Powershell, but I'm not a good Powershell coder.

LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40426020
$servers = @("Server1","Server2","Server3)
$lookbehindduration = 120   # 2 hours ago to now.
foreach ($server in $servers)
Get-SqlErrorLog -sqlserver $server  |
Where-object { (     $_.logdate -ge ((Get-Date).addminutes(-$lookbehindduration)))`
                    -and $_.Text -match '(Error|Fail|IO requests taking longer|is full)' -and $_.Text -notmatch '(without errors|found 0 errors)'}Where-object { (   $_.logdate -ge ((Get-Date).addminutes(-$lookbehindduration)))`
                     -and ($_.text  -notlike '*found 0 errors*')`
                     -and ($_.text  -notlike '*without errors*')`
                     -and ( $_.text -like '*Error*' `
                     -or $_.text -like "*Fail*"`
                     -or $_.text -like "*dump*"`
                     -or $_.text -like '*IO requests taking longer*'`
                     -or $_.text -like '*is full*') `
             } | Out-GridView

Author Comment

by:Abhinav Singh
ID: 40438323
Hello Guys,

I'm really for replying so late.

Kelvin Sparks thanks for replying.
For some reason I'm not able to execute your query and getting the following error:

Msg 213, Level 16, State 7, Procedure xp_readerrorlog, Line 1
Column name or number of supplied values does not match table definition.

Will it be possible for you to arrange the query properly.

Raheman Mohammed Abdul, thank you for providing such a great powershell script.
I'm getting the following error while trying to execute the script:

term 'Get-SqlErrorLog' is not recognized as the name of a cmdlet, function, script file, or operable program

Please look into this.
Again guys, thank you very much.
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40438452
The cmdlet "get-sqlErrorLog" is available from :   (SQL Server Powershell Extentions).
LVL 22

Accepted Solution

Kelvin Sparks earned 500 total points
ID: 40438485
Sorry, I'd given you the SQL Server 2000 version. Here is version for SQL Server 2005 or later

CREATE TABLE #ErrorLog2005
 (LogDate DATETIME, ProcessInfo VARCHAR(50) ,[Text] VARCHAR(4000))
 INSERT INTO #ErrorLog2005
 EXEC sp_readerrorlog
 DELETE FROM #ErrorLog2005
 WHERE ([Text] LIKE '%Intel X86%')
 OR ([Text] LIKE '%Copyright%')
 OR ([Text] LIKE '%All rights reserved.%')
 OR ([Text] LIKE '%Server Process ID is %')
 OR ([Text] LIKE '%Logging SQL Server messages in file %')
 OR ([Text] LIKE '%Errorlog has been reinitialized%')
 OR ([Text] LIKE '%This instance of SQL Server has been using a process ID %')
 OR ([Text] LIKE '%Starting up database %')
 OR ([Text] LIKE '%SQL Server Listening %')
 OR ([Text] LIKE '%SQL Server is ready %')
 OR ([Text] LIKE '%Clearing tempdb %')
 OR ([Text] LIKE '%to execute extended stored procedure %')
 OR ([Text] LIKE '%Analysis of database %')
 OR ProcessInfo = 'Backup' -- Deletes backup information

Author Comment

by:Abhinav Singh
ID: 40441927
Hey Raheman,

Thanks for your reply.

Will it be possible for you to tell me how to configure the above ps1 file after downloading it?
I don't what to do, after downloading the files.
Kindly guide me in this matter.

Thank You.

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question