Reputation: 4726
I've written a series of ASP.NET pages which have several common methods among them. In my ignorance, the best way I was able to find to move those common methods to a separate file was in a Module. However, what I need is a way to identify from which page was the method called?
For example, I have a method call WriteToErrorLog
with the following signature:
WriteToErrorLog(ByVal sender As Page, ByVal Source As String, ByVal Message As String, ByVal StackTrace As String)
Since this is in a Module, the sender Page is unknown. I would like to know if there is a way to access it? I expect that there would be since if an error occurred, the stacktrace ought to reveal the play-by-play of what brought you to the error. Do I really need to pass in the Page or can I retrieve this and the calling function some other way?
Thanks in advance :)
Upvotes: 0
Views: 1247
Reputation: 460340
You can get the reference to the current HttpHandler(the page) even in a static/shared context like in a Module
.
Following is fail-safe even outside of a HttpContext(e.g. class-library referenced from a Winforms project):
If HttpContext.Current IsNot Nothing Then
Dim page = TryCast(HttpContext.Current.Handler, Page)
If page IsNot Nothing Then
' Do something with it '
End If
End If
Edit: You get the name of the calling method in the following way:
Dim method = New StackTrace().GetFrame(1).GetMethod().Name
http://www.csharp-examples.net/reflection-calling-method-name/
Upvotes: 1