Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logging where an error occurred

Status
Not open for further replies.

annethorne

Programmer
Apr 13, 2005
28
US
Hi,

We are currently building a Windows Application with Visual Studio, C# and SQL. Exceptions are caught and logged.
Is there a way for us to determine the exact spot in the code where the error occurred, that is the CLASS and the METHOD?

Here is the code we use now:

catch (Exception error)
{
ExceptionHandler.LogError(error, Applications.ID, UserProfile.UserID, Applications.Version);
throw error;
}

The error object gives us Source and StackTrace, but not location.

Thanks for any help,
:) Anne
 
The stack trace will give you the line numbers in your source code if you include the pdb files. These will be generated automatically from a debug build but you can also get them for a release build.

If you are using Csc.exe you just include the /debug:pdbonly switch. There is a setting in the Visual Studio 2005's project properties dialogue (Build Tab -> Advanced Button -> Debug Info field). You can't set this option in Visual Studio 2003 so you need to use Csc.exe for .Net 1.1 apps.

As a side note you should ensure that any error handlers that re-thow the error just use "throw;" and don't use "throw ex;" as the latter restarts the stack trace.

So use code like this:
Code:
try
{
    // Do some stuff
}
catch (Exception ex)
{
    // Do some error processing
    throw;
}

And not like this:
Code:
try
{
    // Do some stuff
}
catch (Exception ex)
{
    // Do some error processing
    throw ex;
}
 
Are you going to store this information in a database or in a log file or in the event log?

I actually think it's a good idea to store it in a database and in an event log. The database will provide quick access and reporting while the event log provides info to admins.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top