How to write to the event log from code
Posted: 12/20/2004 4:28:50 PMBy: Comfortably Anonymous
Times Read: 2,587
Likes: 0 Dislikes: 0
Topic: Programming: .NET Framework
I'd never tried to do this until today, but it's actually very easy to do. In the example below, I am executing a command on the database inside of a Try/Catch block. In the case that the command fails, the catch block writes details of the exception to the Application Event Log.
Note that you can plug any string into the first parameter, I just have it set to provide the product name of the application (Application.ProductName) which is nicer than a static string for when you embed this code into a reusable class.
Try
oCommand.ExecuteNonQuery()
Catch ex As Exception
strStatus = "Error #" & Str(Err.Number) & " was generated by " & Err.Source & vbCrLf & _
Err.Description & ". The error occurred in function makeConn."
EventLog.WriteEntry(Application.ProductName, strStatus, EventLogEntryType.Error)
End Try
Note that you can plug any string into the first parameter, I just have it set to provide the product name of the application (Application.ProductName) which is nicer than a static string for when you embed this code into a reusable class.
Try
oCommand.ExecuteNonQuery()
Catch ex As Exception
strStatus = "Error #" & Str(Err.Number) & " was generated by " & Err.Source & vbCrLf & _
Err.Description & ". The error occurred in function makeConn."
EventLog.WriteEntry(Application.ProductName, strStatus, EventLogEntryType.Error)
End Try
Rating: (You must be logged in to vote)
Discussion View:
By: Comfortably Anonymous
Times Read: 1,764
Likes: 0 Dislikes: 0
Topic: Programming: .NET Framework
Replies:
RE: How to write to the event log from code
Posted: 3/21/2005 9:42:04 AMBy: Comfortably Anonymous
Times Read: 1,764
Likes: 0 Dislikes: 0
Topic: Programming: .NET Framework
If trying to use this, note that you need to have
include System.Diagnostics
at the top of your code.
include System.Diagnostics
at the top of your code.
Rating: (You must be logged in to vote)