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. How can I get the name of the calling method?

How can I get the name of the calling method?

Scheduled Pinned Locked Moved C#
questiontutorial
4 Posts 2 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.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    In my logging code, I want to display the name of the method that wrote to the log. How can my Log method get the name of the method that called it? I'm thinking of something like the .TargetSite of an Exception (except I'd prefer not to throw an exception :-)... unless that's how to do this).

    J 1 Reply Last reply
    0
    • J JoeRip

      In my logging code, I want to display the name of the method that wrote to the log. How can my Log method get the name of the method that called it? I'm thinking of something like the .TargetSite of an Exception (except I'd prefer not to throw an exception :-)... unless that's how to do this).

      J Offline
      J Offline
      JoeRip
      wrote on last edited by
      #2

      Let me make this more generic: - how, in code, can I stop and get the stack trace up to that line of code? Hopefully I can do this without stopping and throwing an exception and reading the stack trace from the Exception object. I'm sure that's a performance hit...

      N 1 Reply Last reply
      0
      • J JoeRip

        Let me make this more generic: - how, in code, can I stop and get the stack trace up to that line of code? Hopefully I can do this without stopping and throwing an exception and reading the stack trace from the Exception object. I'm sure that's a performance hit...

        N Offline
        N Offline
        NikoTanghe
        wrote on last edited by
        #3

        By using System.Diagnostics.StackFrame like this: public static void Main() { Func(); RL(); } public static void Func() { //Get stack with line numbers System.Diagnostics.StackTrace Stack = new System.Diagnostics.StackTrace(true); foreach(System.Diagnostics.StackFrame Frame in Stack.GetFrames()) { WL(Frame); } }

        J 1 Reply Last reply
        0
        • N NikoTanghe

          By using System.Diagnostics.StackFrame like this: public static void Main() { Func(); RL(); } public static void Func() { //Get stack with line numbers System.Diagnostics.StackTrace Stack = new System.Diagnostics.StackTrace(true); foreach(System.Diagnostics.StackFrame Frame in Stack.GetFrames()) { WL(Frame); } }

          J Offline
          J Offline
          JoeRip
          wrote on last edited by
          #4

          Thanks. That is exactly what I was looking for. Unfortunately, I never got email about your reply! So I went through the Help files and found this myself. I was just coming back to answer my own question when I saw your reply. However, I'm curious how expensive *this* is, too...

          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