Pages

Tuesday, November 3, 2009

About ErrorLogs

To recycle errorlogs:

EXEC sp_cycle_errorlog ;

/*

SQL Server 2005 offers an undocumented system stored procedure sp_readerrorlog.
This SP allows you to read the contents of the SQL Server error log files directly
from a query window and also allows you to search for certain keywords when reading the error file.
This is not new to SQL Server 2005, but this tip discusses how this works for SQL Server 2005.

This procedure takes four parameters:

Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc...
Log file type: 1 or NULL = error log, 2 = SQL Agent log
Search string 1: String one you want to search for
Search string 2: String two you want to search for to further refine the results
If you do not pass any parameters this will return the contents of the current error log.

*/

Drop PROC [dbo].[sp_readerrorlog]
CREATE PROC [dbo].[sp_readerrorlog](
@p1 INT = 0,
@p2 INT = NULL,
@p3 VARCHAR(255) = NULL,
@p4 VARCHAR(255) = NULL)
AS
BEGIN

IF (NOT IS_SRVROLEMEMBER(N'securityadmin') = 1)
BEGIN
RAISERROR(15003,-1,-1, N'securityadmin')
RETURN (1)
END

IF (@p2 IS NULL)
EXEC sys.xp_readerrorlog @p1
ELSE
EXEC sys.xp_readerrorlog @p1,@p2,@p3,@p4
END

--"Error", "Stack Dump", "Dump Thread", "Fatal Exception"
EXEC sp_readerrorlog 0,1,'ERROR','0 to 1'
EXEC sp_readerrorlog 0,1,'Stack Dump'
EXEC sp_readerrorlog 0,1,'Dump Thread'
EXEC sp_readerrorlog 0,1,'Fatal Exception'

No comments:

Post a Comment