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