Examples

Current Articles | Categories | Search

By David Dye @ Sunday, May 04, 2008 5:58 AM :: 1338 Views :: 0 Comments

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' 

Posted @ Sunday, May 04, 2008 5:58 AM by David Dye
Previous Page | Next Page
Comments
Currently, there are no comments. Be the first to post one!
You must be logged in to post a comment. You can login here

Survey

Which of the following CLR objects are you currently using in SQL Server?




Submit Survey  View Results

Links

  Search

What's New

 Subscribe in a reader

Q&A with Adam Machanic - Who's Afraid of SQLCLR by Jonathan Kehayias

You could almost hear the gasps of horror when Microsoft integrated the .NET Common Language Runtime (CLR) into SQL Server 2005. Run C# or VB.NET code inside SQL Server? For many database developers and administrators, it was like an alien had just burst through SQL Server’s midsection.

But SQL Server MVP Adam Machanic says that more and more developers and DBAs are discovering that far from destroying SQL Server performance and security, SQLCLR is actually a powerful ally in solving complex business and technical problems.

Read More on the PASS website..

SQLCLR String Splitting Part 2: Even Faster, Even More Scalable by Site Administrator

SQL Server MVP Adam Machanic shows a new way of parsing strings in SQL Server using SQLCLR that outperforms all conventional TSQL methods, as well as the most common SQLCLR implemenations, using a custom split function that allows streaming of the results.

Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents by Jonathan Kehayias

Learn how to use SQLCLR to get file system information instead of using xp_cmdshell on your SQL Servers.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 0.2808018 seconds.

All information and example code on this site is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from its use.

This site is in no way affiliated with Microsoft. Unless specifically stated otherwise, nothing should be construed to represent the official positions or opinions of Microsoft and/or its Employees.