Home » SQL Server » How to log SQL Server Custom Error in Windows Event Log ?

How to log SQL Server Custom Error in Windows Event Log ?

SQL server gives an option to log custom errors in windows event log. We have 2 options of achieving this.

Option 1 : Using xp_logevent

BEGIN TRY

SELECT 10/0

END TRY

BEGIN CATCH

PRINT ERROR_Message()

DECLARE @msg VARCHAR(100)

SELECT @msg = ERROR_Message()

–xp_logevent error_number , errormessage, severity (INFORMATIONAL, WARNING, or ERROR)

EXEC xp_logevent 60000, @msg, error

END CATCH

Option 2 : Using RAISERROR…..WITH LOG

BEGIN TRY

SELECT 10/0

END TRY

BEGIN CATCH

PRINT ERROR_Message()

DECLARE @msg VARCHAR(100)

SELECT @msg = ERROR_Message()

Leave a Reply

%d bloggers like this: