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()

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 238 other followers

%d bloggers like this: