|
This stored procedure will allow you to write events directly to the Servers Application Log from SQL Server.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.Diagnostics.EventLog
Partial Public Class StoredProcedures
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub log_proc(ByVal logid As Integer, ByVal logentry As String)
Try
'Specify the application log and use the machine name on which the clr proc
'is installed
Dim ev As New EventLog("Application", System.Environment.MachineName, "clr log_proc")
'write to the log the input parameter of logentry, which is the message
'and logid, which is the eventid number recorded in the application log
ev.WriteEntry(logentry, EventLogEntryType.Information, logid)
'If an error occurs send the error message to the messages tab in sql server
Catch ex As Exception
Dim sp As SqlPipe = SqlContext.Pipe()
sp.Send(ex.ToString)
End Try
End Sub
End Class
|
To test this procedure execute the following code in SSMS after creating the procedure:
EXEC log_proc 5115, 'This is a test'
|