Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:31:21+00:00 2026-06-04T19:31:21+00:00

Possible Duplicate: How I can get the calling methods in C# I have a

  • 0

Possible Duplicate:
How I can get the calling methods in C#

I have a Log class that’s supposed to prepend its client’s class and method to the logged message like this:

[ClientClass.ClientMethod] This is the logged message.

And I’m looking for a safe way to obtain these values from the Log class, without passing it as a parameter from the client. I have tried this so far:

var stackFrame = new StackFrame(StackFramesToSkip);
var method = stackFrame.GetMethod();
return string.Format("[{0}.{1}] {2}", method.DeclaringType.Name, method.Name, message);

However, this method doesn’t always yield the calling method. Especially when working with multiple threads.

Is there a safe way of doing this? I’m out of ideas.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:31:22+00:00Added an answer on June 4, 2026 at 7:31 pm

    You could use System.Reflection.MethodBase.GetCurrentMethod() to get the current method’s name and transfer the name to a parameter, but you said you didn’t want those. Well, I know several other ways.

    1. For .NET 4.5 beta, you can use this Caller Information.
    2. You can use a dynamic proxy. The logging method will still require a parameter, but a dynamic proxy allows you to make the invocation only once, and dynamically add it to your methods. You can use ContextBoundObject or Castle Dynamic Proxy
    3. You can use an AOP framework. This allows you to do something similar to (2), but it’s neater. Basically, depending on the framework, the invocation can be added statically instead of dynamically, at build time. Here is a good AOP framework: PostSharp.

    The one time I got to do this, I added the calls by hand. I thought it would be daunting at first, but in my case, it wasn’t really that bad. I had a very limited assortment of methods to log (those that communicated with COM).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can I get all methods of a class? Is there a way
Possible Duplicate: How can I get the client's IP address in ASP.Net MVC? I
Possible Duplicate: Retrieving the calling method name from within a method (C#) I have
Possible Duplicate: How can I find the method that called the current method? How
Possible Duplicate: How can I get a specific parameter from location.search? I have a
Possible Duplicate: Can I get a reference to the 'owner' class during the init
Possible Duplicate: How can I get the client's IP address in a PHP webservice?
Possible Duplicate: Get Class List for Element with jQuery If I have something like
Possible Duplicate: How can I get a list of all the defined variables in
Possible Duplicate: get name of current PHP script in include file Can an included

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.