determining module/class and function/subroutine currently being run

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brad Pears

    determining module/class and function/subroutine currently being run

    I am developing a simple error message class and would like the be able to
    generate as part of the message the curent class/module/form and function or
    sub that the error was generated in without having to hardcode this info to
    be passed to the error message class. I can get the stack trace using
    system.environm ent.stacktrace - but this is too much info. I just want to
    display the function/sub name where the error was generated and the parent
    name of that sub/function

    Any ideas?

    Thanks, Brad

    Thanks, Brad


  • Spam Catcher

    #2
    Re: determining module/class and function/subroutine currently being run

    "Brad Pears" <bradp@truenort hloghomes.comwr ote in
    news:OgKxDwKxHH A.3340@TK2MSFTN GP04.phx.gbl:
    I am developing a simple error message class and would like the be
    able to generate as part of the message the curent class/module/form
    and function or sub that the error was generated in without having to
    hardcode this info to be passed to the error message class. I can get
    the stack trace using system.environm ent.stacktrace - but this is too
    much info. I just want to display the function/sub name where the
    error was generated and the parent name of that sub/function

    System.Reflecti on.MethodBase.G etCurrentMethod .Name will return the current
    procedure name.

    Comment

    • Brad Pears

      #3
      Re: determining module/class and function/subroutine currently being run

      Does it also return who called that procedure at all?

      Brad
      "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
      news:Xns996B94E 4DDEF1usenethon eypotrogers@127 .0.0.1...
      "Brad Pears" <bradp@truenort hloghomes.comwr ote in
      news:OgKxDwKxHH A.3340@TK2MSFTN GP04.phx.gbl:
      >
      >I am developing a simple error message class and would like the be
      >able to generate as part of the message the curent class/module/form
      >and function or sub that the error was generated in without having to
      >hardcode this info to be passed to the error message class. I can get
      >the stack trace using system.environm ent.stacktrace - but this is too
      >much info. I just want to display the function/sub name where the
      >error was generated and the parent name of that sub/function
      >
      >
      System.Reflecti on.MethodBase.G etCurrentMethod .Name will return the current
      procedure name.

      Comment

      • =?Utf-8?B?QU1lcmNlcg==?=

        #4
        Re: determining module/class and function/subroutine currently bei

        Does it also return who called that procedure at all?

        Try:
        Dim sf As StackFrame = New StackFrame(iii, True)
        Dim mb As Reflection.Meth odBase = sf.GetMethod
        where StackFrame is defined in System.Diagnost ics, and iii is the call
        ancestry (0 = current sub/function, 1 = caller, 2 = caller's caller, etc).
        Some useful display strings are:
        mb.DeclaringTyp e().Name()
        mb.Name
        In a debug build, additional useful display strings are:
        sf.GetFileName
        CStr(sf.GetFile LineNumber)

        Comment

        • =?Utf-8?B?TW9kZWxCdWlsZGVy?=

          #5
          Re: determining module/class and function/subroutine currently bei

          You could also use System.Diagnost ics.StackTrace. It surprised me though in
          the following code, the release mode version apparently optimized out
          MethodOne and went directly to MethodTwo. This oddity was in Orcas, so I'd
          be interested if it is not optimized out in VS 2005.

          class Program
          {
          private static void MethodOne()
          {
          MethodTwo();
          }

          private static void MethodTwo()
          {
          System.Diagnost ics.StackTrace st = new
          System.Diagnost ics.StackTrace( );

          Console.Out.Wri teLine(st.ToStr ing());
          System.Threadin g.Thread.Sleep( 5000);
          }

          static void Main(string[] args)
          {
          MethodOne();
          }
          }


          "AMercer" wrote:
          Does it also return who called that procedure at all?
          >
          Try:
          Dim sf As StackFrame = New StackFrame(iii, True)
          Dim mb As Reflection.Meth odBase = sf.GetMethod
          where StackFrame is defined in System.Diagnost ics, and iii is the call
          ancestry (0 = current sub/function, 1 = caller, 2 = caller's caller, etc).
          Some useful display strings are:
          mb.DeclaringTyp e().Name()
          mb.Name
          In a debug build, additional useful display strings are:
          sf.GetFileName
          CStr(sf.GetFile LineNumber)
          >

          Comment

          • =?Utf-8?B?TW9kZWxCdWlsZGVy?=

            #6
            Re: determining module/class and function/subroutine currently bei

            Oops! Sorry about the incorrect language, but the VB should be similar!

            "ModelBuild er" wrote:
            You could also use System.Diagnost ics.StackTrace. It surprised me though in
            the following code, the release mode version apparently optimized out
            MethodOne and went directly to MethodTwo. This oddity was in Orcas, so I'd
            be interested if it is not optimized out in VS 2005.
            >
            class Program
            {
            private static void MethodOne()
            {
            MethodTwo();
            }
            >
            private static void MethodTwo()
            {
            System.Diagnost ics.StackTrace st = new
            System.Diagnost ics.StackTrace( );
            >
            Console.Out.Wri teLine(st.ToStr ing());
            System.Threadin g.Thread.Sleep( 5000);
            }
            >
            static void Main(string[] args)
            {
            MethodOne();
            }
            }
            >
            >
            "AMercer" wrote:
            >
            Does it also return who called that procedure at all?
            Try:
            Dim sf As StackFrame = New StackFrame(iii, True)
            Dim mb As Reflection.Meth odBase = sf.GetMethod
            where StackFrame is defined in System.Diagnost ics, and iii is the call
            ancestry (0 = current sub/function, 1 = caller, 2 = caller's caller, etc).
            Some useful display strings are:
            mb.DeclaringTyp e().Name()
            mb.Name
            In a debug build, additional useful display strings are:
            sf.GetFileName
            CStr(sf.GetFile LineNumber)

            Comment

            • Phill W.

              #7
              Re: determining module/class and function/subroutine currently beingrun

              Brad Pears wrote:
              I am developing a simple error message class and would like the be able to
              generate as part of the message the curent class/module/form and function or
              sub that the error was generated in without having to hardcode this info to
              be passed to the error message class.
              System.Reflecti on.MethodBase.G etCurrentMethod ().Name

              gets you the name of the currently executing method, but you have to
              code it into every method. Tedious.
              I can get the stack trace using system.environm ent.stacktrace
              - but this is too much info.
              You /can/ get the StackTrace and pull it to pieces to extract the bit(s)
              you're after. You might add this to the constructor (Sub New) of your
              "error message class":

              Dim sf As System.Diagnost ics.StackFrame _
              = New System.Diagnost ics.StackFrame( 1, False)
              Dim method As System.Reflecti on.MethodBase _
              = sf.GetMethod()

              ? method.Reflecte dType.FullName
              ? method.Name
              I just want to display the function/sub name where the error was generated
              and the parent name of that sub/function
              IMHO, if you're using this to track down errors, then you /need/ the
              /entire/ stack trace (and more). The logging class I use gets laced
              into each and every method and is used to record the entry and exit
              points of those methods, complete with arguments and return values.
              Without /all/ of this, it's .. rather difficult, shall we say .. to get
              to the bottom of what's going wrong.

              HTH,
              Phill W.

              Comment

              • Brad Pears

                #8
                Re: determining module/class and function/subroutine currently being run

                Any chance of getting a copy of that logging class you use and some
                instructions on it's implementation and use? Sounds like something I might
                want to use... :-)

                Thanks, Brad

                "Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
                news:f7fvl5$6sj $1@south.jnrs.j a.net...
                Brad Pears wrote:
                >
                >I am developing a simple error message class and would like the be able
                >to generate as part of the message the curent class/module/form and
                >function or sub that the error was generated in without having to
                >hardcode this info to be passed to the error message class.
                >
                System.Reflecti on.MethodBase.G etCurrentMethod ().Name
                >
                gets you the name of the currently executing method, but you have to code
                it into every method. Tedious.
                >
                >I can get the stack trace using system.environm ent.stacktrace
                >- but this is too much info.
                >
                You /can/ get the StackTrace and pull it to pieces to extract the bit(s)
                you're after. You might add this to the constructor (Sub New) of your
                "error message class":
                >
                Dim sf As System.Diagnost ics.StackFrame _
                = New System.Diagnost ics.StackFrame( 1, False)
                Dim method As System.Reflecti on.MethodBase _
                = sf.GetMethod()
                >
                ? method.Reflecte dType.FullName
                ? method.Name
                >
                >I just want to display the function/sub name where the error was
                >generated
                >and the parent name of that sub/function
                >
                IMHO, if you're using this to track down errors, then you /need/ the
                /entire/ stack trace (and more). The logging class I use gets laced into
                each and every method and is used to record the entry and exit points of
                those methods, complete with arguments and return values. Without /all/ of
                this, it's .. rather difficult, shall we say .. to get to the bottom of
                what's going wrong.
                >
                HTH,
                Phill W.

                Comment

                Working...