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 376635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:37:06+00:00 2026-05-12T14:37:06+00:00

Is it possible to profile individual methods via attributes in .NET? I am currently

  • 0

Is it possible to profile individual methods via attributes in .NET?

I am currently trying to locate some of the bottle necks in a large legacy application that makes heavy use of static methods. Integrating a framework is simply not an option at the moment. Since most of the calls use static methods, interfaces and dependency injection are not available. Hacking the code to log diagnostics is not a viable solution either.

I know that there are some profiling tools on the market, but they are currently outside of the budget. Ideally, I would be able to create my own custom attribute that would log some basic information on method entry and method exit. I’ve never really worked with custom attributes so any insight into if this is even possible would be appreciated.

If possible, I’d like to be enable the profiling via a config file. This would support profiling via unit and integration tests.

  • 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-05-12T14:37:06+00:00Added an answer on May 12, 2026 at 2:37 pm

    You can’t use attributes for what you are doing. However, you do have some choices:

    First, many of the profiler tools out there (like RedGate ANTS) are relatively inexpensive ($200-$300), easy to use, and most offer free evaluation periods of a couple of weeks – so you can see if they will give you the lift you need now, before you decide whether to buy them. Also, the .NET CLR profiler is free to download.

    If that’s not possible, PostSharp is probably the easiest way to weave such logic into your code.

    Lastly, if you can’t use PostSharp for whatever reason and you are willing to go and add attributes to your code, you may as well add a simple instrumentation block to each method in the form of a using block:

    public void SomeMethodToProfile()
    {
        // following line collects information about current executing method
        // and logs it when the metric tracker is disposed of
        using(MetricTracker.Track(MethodBase.GetCurrentMethod()))
        { 
            // original code here...
        }
    }
    

    A typical MetricTracker implementation looks something like this:

    public sealed class MetricTracker : IDisposable
    {
        private readonly string m_MethodName;
        private readonly Stopwatch m_Stopwatch;
    
        private MetricTracker( string methodName ) 
           { m_MethodName = methodName; m_Stopwatch = Stopwatch.StartNew(); }
    
        void IDisposable.Dispose()
           { m_Stopwatch.Stop(); LogToSomewhere(); }
    
        private void LogToSomewhere()
           { /* supply your own implementation here...*/ }
    
        public static MetricTracker Track( MethodBase mb )
           { return new MetricTracker( mb.Name ); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to assign Profile values? I'm using ASP.NET MVC and the Membership
Possible Duplicate: How to assign Profile values? I'm reading an ASP.NET book that says
Possible Duplicate: Bug with starting Instruments via Profile under XCode 4.1 Build 4B110 Instruments
Possible Duplicate: Difference between .NET 4 Client Profile and Full Framework download I noticed
Possible Duplicate: Problem getting Facebook pictures via iOS How to get user's facebook profile
Is it possible to personalize the My Profile page in the same way it
I am wondering if it is possible to leverage the Authentication, Membership, and/or Profile
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Into I have two tables profile and name , the profile table contains some
Possible Duplicate: How to profile and and get Javascript performance I have a page

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.