Enable/Disable Aspect at Runtime

ON's Avatar

ON

17 May, 2017 12:12 AM

Hello,

I was wondering if there was a way I could enable/disable an aspect at runtime. I have a MethodBoundryAspect that runs all over my project, if for some reason i would like to disable it what would be the best way to go about doing that without disrupting too much? Since this Aspect runs against all of the methods in multiple projects I would need something that wouldn't create a performance hit. I was wondering if there was a PostSharp configuration value that could be changed to enable/disable the aspect.

Here is an example of my aspect:

public sealed class MyAspect : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
             //Logic here to check if the Aspect is enable or disabled
    }

    public override void OnExit(MethodExecutionArgs args)
    {
             //Logic here to check if the Aspect is enable or disabled
    }
}

I define it the project's AssemlyInfo.cs file like so


  [assembly: MyAspect]

Thanks

  1. Support Staff 1 Posted by PostSharp Techn... on 17 May, 2017 03:07 PM

    PostSharp Technologies's Avatar

    Hello,

    Your example is the only way how to enable/disable an aspect on runtime. Unfortunately there is no way how to completely avoid runtime overhead of a disabled aspect without rebuilding the application.

    You can mitigate the runtime overhead by taking advantage of Aspect Optimizer. Aspect Optimizer optimizes out unused fields of MethodExecutionArgs. You may also want to try pre-process as much as possible in CompileTimeInitialize which runs during compilation.

    If you want to traces method execution, you may try revamped PostSharp.Patterns.Diagnostic Log aspect in PostSharp 5.0. Unfortunately we don't have complete documentation yet for the feature, but you can take a look at this knowledge page.

    -jakub

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac