xp_logevent Extended Stored Procedure

Logs a user-defined message in the SQL Server log file and/or in the Windows NT Event Viewer. When sending messages from Transact-SQL procedures, triggers, batches, and so on, use the RAISERROR statement instead of xp_logevent. If raising an error to the client is not desired, xp_logevent can be used to send an alert without sending a message to the client.

Syntax

xp_logevent error_number, message, [severity]

where

error_number
Is a user-defined error number greater than 50,000. The maximum value is 1,073,741,823 (2 (30) - 1).
message
Is a character string less than 255 characters. This message is logged in the SQL Server log file and/or in the Windows NT Event Viewer.
severity
Is one of three character strings: Informational, Warning, or Error. This parameter is optional; the default is Informational.

Remarks

The xp_logevent extended stored procedure does not call a client's message handler or set @@ERROR. To write messages to the Windows NT Event Viewer and/or to the SQL Server error log file with SQL Server 6.0, you can execute the RAISERROR statement. For details, see the RAISERROR statement.

Example

This example logs the message (with variables passed to the message) in the Windows NT Event Viewer.

DECLARE @tabname varchar(30)
DECLARE @username varchar(30)
DECLARE @message varchar(255)
SELECT @tabname = 'titles'
SELECT @username = USER_NAME()
SELECT @message = 'The table "'  @tabname  '" is not owned by the user 
    "'  @username  '."'

EXEC xp_logevent 60000, @message, informational

Permission

Execute permission defaults to the system administrator and can be granted to other users.

See Also

PRINT RAISERROR