Finding current Procedure

  • Thread starter Thread starter Tor Inge Rislaa
  • Start date Start date
T

Tor Inge Rislaa

Finding current Procedure

I want to write errors to a log-file, where the error log contain the
description of where the error occurred and what kind of error it was.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

On Error GoTo errHandler

'Some code

If Somthing Then

"do this"

Else

"do that"

End If

Exit Sub

errHandler:

myPrintToFileProcedure("Error:" & Err.Number & " - " & Err.Description & "
in procedure Button1_Click")

End Sub

Is it possible to get the text Button1_Click dynamically from the procedure
that raise the error, so that this don't need to be hardcoded?

TIRislaa
 
* "Tor Inge Rislaa said:
I want to write errors to a log-file, where the error log contain the
description of where the error occurred and what kind of error it was.

\\\
MsgBox(System.Reflection.MethodBase.GetCurrentMethod().Name)
///
 
Tor,
I would use a Try/Catch and use the Exception.ToString method to get the
FULL stack trace along with the message description & other information that
the Exception itself may be offering:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Try
'Some code
If Somthing Then
"do this"
Else
"do that"
End If
Catch ex As Exception
myPrintToFileProcedure(ex.ToSTring())
End Try
End Sub

However!!! I would not include the above code in each event handler. I would
include it once in the Application.ThreadException event.

Something like:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'Some code
If Somthing Then
"do this"
Else
"do that"
End If
End Sub


Public Shared Sub Main()
AddHandler Application.ThreadException, AddressOf OnThreadException
Application.Run(New MainForm)
End Sub

Private Sub OnThreadException(sender As Object, _
ByVal e As System.Threading.ThreadExceptionEventArgs)
Try

myPrintToFileProcedure(ex.ToSTring())
Catch ex As Exception
' Don't want to end the app here! ;-)
End Try
End Sub



Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

Hope this helps
Jay
 
In reading that I notice that it "should not be deployed to end-users."

If my program as distributed generates an error log that I want the
end-user to send to me, I would I want the line number, right?

There must be a good reason for you to give that advice, but other than
sparing the size of the .pdb file on the end-user machine I can't see it.
Can you explain the reason further?

Regards,
Ot
 
* "Ot said:
In reading that I notice that it "should not be deployed to end-users."

If my program as distributed generates an error log that I want the
end-user to send to me, I would I want the line number, right?

There must be a good reason for you to give that advice, but other than
sparing the size of the .pdb file on the end-user machine I can't see it.
Can you explain the reason further?

Maybe performance will be not as good as in the release version and
recreation of the source code will be easier with the debug information.
 
Back
Top