|
-
Nov 28th, 2009, 03:00 PM
#1
Thread Starter
Junior Member
[RESOLVED] Catching Errors with a Errorhandler
Hi,
Does someone have a example to catch errors and log them to the Eventlog?
I would like to catch any error and pass them to my WriteToEventLog function.
If i use On Error , how could I catch the Error and pass it to my WriteToEventLog function?
I would like to catch the actual error
Public Function WriteToEventLog(ByVal entry As String, Optional ByVal appName As String = "CompanyName", Optional ByVal eventType As EventLogEntryType = EventLogEntryType.Information, Optional ByVal logName As String = "ProductName") As Boolean
'*************************************************************
'NAME: WriteToEventLog
'PURPOSE: Write to Event Log
'PARAMETERS: Entry - Value to Write
' AppName - Name of Client Application. Needed
' because before writing to event log, you must
' have a named EventLog source.
' EventType - Entry Type, from EventLogEntryType
' Structure e.g., EventLogEntryType.Warning,
' EventLogEntryType.Error
' LogNam1e: Name of Log (System, Application;
' Security is read-only) If you
' specify a non-existent log, the log will be
' created
'RETURNS: True if successful
'*************************************************************
Dim objEventLog As New EventLog
Try
'Register the Application as an Event Source
If Not EventLog.SourceExists(appName) Then
EventLog.CreateEventSource(appName, logName)
End If
'log the entry
objEventLog.Source = appName
objEventLog.WriteEntry(entry, eventType)
Return True
Catch Ex As Exception
Return False
End Try
End Function
-
Nov 28th, 2009, 03:12 PM
#2
Re: Catching Errors with a Errorhandler
you are catching the error... that's what the "Catch ex As Exception" is.... that's your catch. Before you return, that's where you should be logging the error at.
OH, I see what you posted now... in that case, you have an example of how to catch errors.... it's the "Try...Catch...Finally" syntax.
-tg
-
Nov 28th, 2009, 03:21 PM
#3
Thread Starter
Junior Member
Re: Catching Errors with a Errorhandler
 Originally Posted by techgnome
you are catching the error... that's what the "Catch ex As Exception" is.... that's your catch. Before you return, that's where you should be logging the error at.
OH, I see what you posted now... in that case, you have an example of how to catch errors.... it's the "Try...Catch...Finally" syntax.
-tg
Yes you are right, but i want to pass the exeption to my WritetoEvenlog function..
and
i got this error when trying to convert it to string.
"Value of type 'System.Exception' cannot be converted to 'String"
Maybe my Iam on the wrong track..
My goal is to pass it to the eventlog.
Thanks
/Stefan
-
Nov 28th, 2009, 03:24 PM
#4
Thread Starter
Junior Member
Re: Catching Errors with a Errorhandler
Like this
Dim ex as Exeption
On error goto Errorhandler
Sub Errorhandler (Error as exeption)
msgbox"Error blabla"
WriteToEventLog(Ex, "Myapp", EventLogEntryType.Error, "Application")
Exit Sub
-
Nov 28th, 2009, 03:33 PM
#5
Re: Catching Errors with a Errorhandler
Just an example....
VB.NET Code:
Dim myEvLog As New EventLog("MineLog", ".", "MySource")
Try
Dim i As Integer = Integer.MaxValue - 1
'Something that raises an error
i += 10
Catch ex As Exception 'Error occurred
myEvLog.WriteEntry(ex.Message, EventLogEntryType.Error)
End Try
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Nov 28th, 2009, 03:35 PM
#6
Re: Catching Errors with a Errorhandler
no.....
Code:
Try
'Do some code in here that could go wrong
Catch ex As Exception
WriteToEventLog(Ex.Message, "Myapp", EventLogEntryType.Error, "Application")
End Try
It's exactly the same as the example that is in your logger that you posted.
-tg
-
Nov 28th, 2009, 04:24 PM
#7
Thread Starter
Junior Member
Re: Catching Errors with a Errorhandler
Thanks!!!
So easy..
I Love this forum and all helpfull people!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|