473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

get line number in error handling code

I want to get the file name, line number etc. in error handling code in an
asp.net application. I know how to trap errors in the Global.asax and I read
that you can use the stack frame from the System.Diagnostics namespace to get
line numbers provided you release in debug mode but when I use the code below
I merely get the line number, file name etc. of the Global.asax page, not the
page that produced the error. Any way to do this?

protected void Application_Error(Object sender, EventArgs e)

{

string errorLine;

string fileName;

string functionName;

StackTrace st=new StackTrace (0,true);

StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);

fileName = sf.GetFileName ();

errorLine = sf.GetFileLineNumber().ToString();

functionName = sf.GetMethod().ToString();

}


--
Scott
Mar 7 '06 #1
1 5117
Hi Scott,

Welcome to the ASP.NET newsgroup.

For the component classes in the System.Diagnostics namespace, they provide
the function to inspect the current executing thread's code path and
structure. However, for your scenario, the when you use the
System.Diagnostics components in Application_Error event, it can only
display execution info of the current executiion funciton( the
Application_Error event handler) rather than the unhandled exception
because the exception has occuried before the application_Error event be
fired.

To get the information for the unhandled exception, I think you should use
the Server.GetLastError() method to get the exception object reference and
them get the detailed information of the exception through the Exception
object's properties. For example:

void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();

Context.Items.Add("current_exception", Server.GetLastError());

Server.Transfer("~/MyCustomErrorPage.aspx");

}

then, in my custom error page, we can printout the unhandled exception info:

protected void Page_Load(object sender, EventArgs e)
{
Exception ex = Context.Items["current_exception"] as Exception;

if (ex != null)
{
Response.Write("<br/>-----------------------------");
Response.Write("<br/>Source: " + ex.Source);
Response.Write("<br/>-----------------------------");
Response.Write("<br/>StackTrace: " + ex.StackTrace);
Response.Write("<br/>-----------------------------");

}
}

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Mar 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kim Haines | last post by:
I need help finding where an error is occuring in my code. I use a try-catch block like this in my global.asa: try { //my code } catch (e) { Application('errormsg') = ("An exception...
2
by: Ben | last post by:
Hello all After years of playing developing stuff for fun and personal use I'm finally developing an app that I am intending to sell. As such, I'm trying to make it as reliable and stable as...
5
by: Joe Delphi | last post by:
Hi Newbie to VB.Net and I have a question I need to open a text file, read each line, and if I find something in the line, delete that line from the text file. Can anyone tell me how to do...
0
by: Me | last post by:
I am writing a web application and I want to catch and rethrow every unhandled exception to the Application_Error. However, the line number indicated in the stack trace is from the Throw line,...
3
by: Rotsey | last post by:
Hi, I am trying to write some error handling code to log the error. But getting the line number of the error is not simple. Is it because the app is set to "Release" and does not have debug...
4
by: Jeff Jarrell | last post by:
I have a block of code that during development is prone to casting errors. It is mostly a DataReader type thing. It looks something like this. _prtPNID = myDLReader.GetString("prtPNID")...
3
by: Mark | last post by:
Hi All, I have wrote a sub to record events from within the database. A lot of these events are errors. The sub has the module name and function/sub name passed to it to record to the table. My...
9
by: Smithers | last post by:
Please consider this humble method: public void ResetCounters() { m_TotalExceptionsDetected = 0; m_TotalMessagesSent = 0; } Given no further information, would you wrap those two lines in a...
2
by: =?Utf-8?B?TXIgTWFqb3IgVGhvcmJ1cm4=?= | last post by:
I am using global.asax to handle my web application exceptions and have configured it to create event log entries for errors that occur and to display an error page advising the user to contact the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.