Examples

Current Articles | Categories | Search

By David Dye @ Sunday, May 04, 2008 5:58 AM :: 201 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

Vote For It - Server Side Compiles in SQL CLR by Jonathan Kehayias

Greg Low a SQL Server MVP, has submitted a very compelling Connect item 265266, Add server-side compilation ability to SQL CLR which aims to remove the requirement to develop SQLCLR assemblies external to SQL Server.  Read about this suggestion here, and vote for it by visiting the Connect site to let Microsoft know you think it is important.

Using CLR Impersonation to Access Resources Outside of SQL Server by Jonathan Kehayias

Traditionally if you had a need to access a file or other resource outside of SQL Server, the SQL Server service account was required to have appropriate file system access to the folder or path containing the file.  With CLR integration, this is no longer an absolute requirement.  Identity Impersonation will allow you to implicitly or explicitly change the execution context inside of a SQLCLR Function, Procedure, or Trigger. 

Using an Application Configuration (app.config/web.config) File in SQL Server CLR Integration by Jonathan Kehayias

A common part of programming in .NET is to use an configuration file to store configuration information in an easily modifiable location.  The app.config or web.config file is an invaluable inclusion in most .NET projects and developers may need to maintain this functionality as a part of logic sharing between objects in the database and the application as well.  This article will demonstrate how to configure your SQLCLR project to use Configuration Files in SQL.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 1.0468549 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.