SQL Server 寫入 Event Log的兩種方法

SQL provides us to make an entry in Event Viewer by two ways:
1. using XP_LogEvent

將使用者自訂訊息記錄在 SQL Server 記錄檔以及 Windows 事件檢視器中。xp_logevent 可以用來傳送警示而不傳送訊息給用戶端。

error_number:這是使用者自訂的錯誤號碼 (大於 50,000)。最大值是 2147483647 (2^31 – 1)。
‘message’:這是字元字串,最多 255 個字元。
‘severity’:這是下列三個字元字串之一:INFORMATIONAL、WARNING 或 ERROR。severity 是選擇性的,預設值是 INFORMATIONAL。

微軟備註
從 Transact-SQL 程序、觸發程序、批次等傳送訊息時,請利用 RAISERROR 陳述式來取代 xp_logevent。xp_logevent 不會呼叫用戶端的訊息處理常式或設定 @@ERROR。若要將訊息寫入 Windows 事件檢視器和 SQL Server 執行個體內的 SQL Server 錯誤記錄檔中,請執行 RAISERROR 陳述式。

2. Raiserror WITH LOG ()
RAISERROR(‘information’,15,3) WITH LOG; –寫入資訊等級為 資訊(<=15)
RAISERROR('warning',16,3) WITH LOG; –寫入資訊等級為 警告(=16)
RAISERROR(‘error’,17,3) WITH LOG; –寫入資訊等級為 錯誤(>16)

This entry was posted in 個人雜記 and tagged . Bookmark the permalink.

發表迴響

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 / 變更 )

Twitter picture

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

Facebook照片

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

連結到 %s