Technical Article

Search SQL Error Log

,

Description

This stored procedure is based on the native SQL sp sp_readerrorlog, but with the addition of selecting all error logs for searching string patterns. It also has the flexibility of knowing the amount of error log files configured, so in case you specify an invalid number, the sp will let you know the valid numbers.

The script is executed with the following parameters:

  • @AllLogs
  • @LogFile
  • @SearchString1
  • @SearchString2

The @AllLogs parameter is used to determine if the search will be performed on all error log files. In case its value is 0, then the parameter @LogFile is used to know on which error log file to search. If @AllLogs is 0, then @LogFile cannot be null. If is 1, then @LogFile is ignored.

Finally, there are two parameters (@SearchString1 and @SearchString2) that represent the strings to look for. If no value is specified, all records will be returned.

Examples

Assuming you have SQL Server configured with the default quantity of error log files (6) and you perform the following query, exec sp_SearchSQLErrorLog @AllLogs = 0, @LogFile = 6, @SearchString1 = 'login', @SearchString2 = 'sa', then you'll get all records from the sixth log file containing the words login and sa.

If you run the same query but changing the @LogFile parameter to 7, then you'll receive a message error like the following:

Msg 50000, Level 11, State 3, Procedure sp_SearchSQLErrorLog, Line 63
You must specify a valid log file. The logfile 7 is invalid. Valid values are 1 to 6

 

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Alejandro Pelc
-- Create date: 03/05/2009
-- Description: Procedure to search in Error Log
-- =============================================
CREATE PROCEDURE sp_SearchSQLErrorLog
@AllLogs bit,
@LogFile int = null,
@SearchString1 varchar(255) = null,
@SearchString2 varchar(255) = null
AS
BEGIN
SET NOCOUNT ON
if((@AllLogs = 0) and (@LogFile is null))
begin
RAISERROR (N'You must specify a valid log file.',11, 1)
end
else
begin
declare @tblerrors table(
logfile tinyint null,
logdate datetime,
process varchar(200),
descript nvarchar(max)
)
declare @msg varchar (100)
declare @NumErrorLogs int
declare @loop tinyint
exec master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'NumErrorLogs', @NumErrorLogs OUTPUT
if(@AllLogs = 1)
begin
set @loop = 0
while(@loop < @NumErrorLogs)
begin
insert into @tblerrors (logdate,process,descript)
exec('sp_readerrorlog '+ @loop + ',1,''' + @SearchString1 + ''', ''' + @SearchString2 + '''')
update @tblerrors set logfile = @loop + 1 where logfile is null
set @loop = @loop + 1
end
if exists(select 1 from @tblerrors)
select logfile as 'Log File N°',logdate as 'Date',process as 'Process',descript as 'Message' from @tblerrors order by logdate
else
print 'No records found that matches your search criteria.'
end
else
begin
if(@LogFile<>0)
begin
set @LogFile = @LogFile - 1
if(@LogFile < @NumErrorLogs)
begin
insert into @tblerrors (logdate,process,descript)
exec('sp_readerrorlog '+ @LogFile + ',1,''' + @SearchString1 + ''', ''' + @SearchString2 + '''')
update @tblerrors set logfile = @LogFile + 1 where logfile is null
if exists(select 1 from @tblerrors)
select logfile as 'Log File N°',logdate as 'Date',process as 'Process',descript as 'Message' from @tblerrors order by logdate
else
print 'No records found that matches your search criteria.'
end
else
begin
set @msg = 'You must specify a valid log file. The logfile ' + cast(@LogFile as nvarchar) + ' is invalid. Valid values are 1 to ' + cast(@NumErrorLogs as nvarchar)
RAISERROR (@msg,11, 3)
end
end
else
begin
set @msg = 'You must specify a valid log file. The logfile ' + cast(@LogFile as nvarchar) + ' is invalid. Valid values are 1 to ' + cast(@NumErrorLogs as nvarchar)
RAISERROR (@msg,11, 2)
end
end
end
END

Rate

5 (4)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (4)

You rated this post out of 5. Change rating