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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:44:33+00:00 2026-05-25T14:44:33+00:00

I currently have an action that I perform on every method in a class

  • 0

I currently have an action that I perform on every method in a class as follows:

public void DoSomething(object thing)
    {
        LogHelper.LogExceptions(() =>
        {
            //DoSomethingWithThing
        });
    }

My method contents are all inside the action for LogExceptions. I did this so I didn’t have to have a lot of try, catches and throws all over the show. It works exactly as I want it to, but being the lazy man that I am, I don’t want to keep having to write out the LogExceptions call and nesting my logic in it.

What I want to do is pull up that logic to an attribute, so all I need to do is something like:

    [LogExceptions()]
    public void DoSomething(object thing)
    {
         //DoSomethingWithThing
    }

Firstly, is this possible, and secondly, does anyone know of any reference material I could use?

Many 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-05-25T14:44:34+00:00Added an answer on May 25, 2026 at 2:44 pm

    Have a look at an Aspect Oriented Programming (AOP) Framework such as PostSharp. PostSharp allows you to create attributes which you can use to decorate classes, methods…etc.

    Such an attribute (or aspect) can be programmed to inject code before your method executes and after its finished.

    For example, logging using attributes:

    http://www.sharpcrafters.com/solutions/logging

    To be complete I’ll include a small example. Let’s say we want to log a message each time a method executes. You can create an aspect (PostSharp attribute) for this purpose.

    [Serializable]
    public class LogAttribute : OnMethodBoundaryAspect
    {
        public override void OnExit(MethodExecutionArgs args)
        {
            base.OnExit(args);
    
            Console.WriteLine(String.Format("{0}.{1}: {2}",
                args.Method.DeclaringType.Name,
                args.Method.Name,
                args.ReturnValue));
        }
    }
    

    Here the aspect writes out a message to the console window when the method is finished.

    Now you can apply this attribute to a method.

    public class Calculator
    {
        [Log]
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
    

    Voila, this makes your method more readable as it is not being cluttered by logging code which adds nothing to the actual functionality of the method (adding two numbers). This results in more readable and maintainable code.

    A while back I wrote an article about it on my blog. It’s quite lenghty to copy here, but it demonstrates how to build such attributes and to replace common code such as logging, exception handling…etc. by attributes so that you don’t clutter all of your method with this kind of code:

    https://github.com/geersch/PostSharp

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

Sidebar

Related Questions

I have a standard textbox that I want to perform an action on a
Currently, I have a controller action that outputs a PDF (as a Response.OutputStream.Write() )
I have a custom validation attribute derived from action filter attribute. Currently the attribute
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have a class and I'm trying to create an easy GUI to
We currently have a production application that runs as a windows service. Many times
I currently have a Perl script that runs an external command on the system,
I currently have a gridview that has an asp:ButtonField as one of the columns.
I currently have some code which needs to perform multiple updates per user for
I currently have an ASP.NET GridView that displays a few columns to the user

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.