Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Logging Class and Method where exception occurred.

Logging Class and Method where exception occurred.

Scheduled Pinned Locked Moved C#
csharphelpdatabasevisual-studioquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnneThorne
    wrote on last edited by
    #1

    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

    T P 2 Replies Last reply
    0
    • A AnneThorne

      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

      T Offline
      T Offline
      Tarakeshwar Reddy
      wrote on last edited by
      #2

      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 --

      P 1 Reply Last reply
      0
      • T Tarakeshwar Reddy

        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 --

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A AnneThorne

          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

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          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+.

          P 1 Reply Last reply
          0
          • P PIEBALDconsult

            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+.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups