Logging Class and Method where exception occurred.
-
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 -
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, :) AnneAnneThorne wrote:
catch (Exception error) { ExceptionHandler.LogError(error, Applications.ID, UserProfile.UserID, Applications.Version); throw error; }
I would probably suggest changing your function to take two more parameters 'this'(which would give you the name of the class sending you error) and a string to have the method name.
ExceptionHandler.LogError(error, Applications.ID, UserProfile.UserID, Applications.Version,this,"this method">");
you can actually use the StackFrame class in "System.Diagnostics;" to get the information you want. you can change your function to take 2 more parameters
ExceptionHandler.LogError(error, Applications.ID, UserProfile.UserID, Applications.Version, this, new StackFrame().GetMethod().Name);
Last modified: 9mins after originally posted --
-
AnneThorne wrote:
catch (Exception error) { ExceptionHandler.LogError(error, Applications.ID, UserProfile.UserID, Applications.Version); throw error; }
I would probably suggest changing your function to take two more parameters 'this'(which would give you the name of the class sending you error) and a string to have the method name.
ExceptionHandler.LogError(error, Applications.ID, UserProfile.UserID, Applications.Version,this,"this method">");
you can actually use the StackFrame class in "System.Diagnostics;" to get the information you want. you can change your function to take 2 more parameters
ExceptionHandler.LogError(error, Applications.ID, UserProfile.UserID, Applications.Version, this, new StackFrame().GetMethod().Name);
Last modified: 9mins after originally posted --
That appears to give the name of the method that's calling LogError, but the exception may have occured deeper and been bubbled up until finally caught. This is something that I'm trying to work out as well.
-
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, :) AnneTry
new System.Diagnostics.StackTrace ( error ).GetFrames()[0].GetMethod().Name
the only shortcoming is that it requires debug info to do what I want. Generally release builds don't include debug info. I'm still trying to figure the proper settings for a VS project. When I compile on the command line (with CSC) the switch is/debug+
. -
Try
new System.Diagnostics.StackTrace ( error ).GetFrames()[0].GetMethod().Name
the only shortcoming is that it requires debug info to do what I want. Generally release builds don't include debug info. I'm still trying to figure the proper settings for a VS project. When I compile on the command line (with CSC) the switch is/debug+
.namespace Template
{
public partial class Template
{
private static int
div
(
int lhs
,
int rhs
)
{
return ( lhs / rhs ) ;
}\[System.STAThread\] public static int Main ( string\[\] args ) { int result = 0 ; try { int x = int.Parse ( args \[ 0 \] ) ; int y = int.Parse ( args \[ 1 \] ) ; int z = div ( x , y ) ; } catch ( System.Exception err ) { while ( err != null ) { string sep = " ( " ; System.Reflection.MethodBase meth = new System.Diagnostics.StackTrace ( err ).GetFrames()\[0\].GetMethod() ; System.Console.Write ( meth.DeclaringType.FullName + "." + meth.Name ) ; foreach ( System.Reflection.ParameterInfo parm in meth.GetParameters() ) { System.Console.Write ( sep ) ; if ( parm.IsOut ) { System.Console.Write ( "out" ) ; } System.Console.Write ( parm.Name + " " + parm.ParameterType.Name ) ; sep = " , " ; } System.Console.WriteLine ( " ) " + err.Message ) ; err = err.InnerException ; } } return ( result ) ; } }
}
C:\>err
Template.Template.Main ( args String[] ) Index was outside the bounds of the array.C:\>err jkfg
System.Number.StringToNumber ( str String , options NumberStyles , number NumberBuffer& , info NumberFormatInfo , parseDecimal Boolean ) Input string was not in a correct format.C:\>err 1 jkfg
System.Number.StringToNumber ( str String , options NumberStyles , number NumberBuffer& , info NumberFormatInfo , parseDecimal Boolean ) Input string was not in a correct format.C:\>err 1
Template.Template.Main ( args String[] ) Index was outside the bounds of the array.C:\>err 1 jkfg
System.Number.StringToNumber ( str String , options NumberStyles , number NumberBuffer& , info NumberFormatInfo , parseDecimal