PatrickNolan
PatrickNolan

Reputation: 1979

Applying PostSharp aspect via AssemblyInfo

I want to apply my VerboseTraceAspect to my solution, and apply the attribute everywhere except for

  1. Getters and setters
  2. Any type in TestProject.Logging.* and TestProject.Tracing.*

I am using the following sample but it does not seem to work. Am I doing this wrong? If so, how should it be done?

Thanks heaps.

[assembly: VerboseTraceAspect(AspectPriority = 0, AttributeExclude = true, AttributeTargetTypes = "TestProject.Logging.*|TestProject.Tracing.*")]

[assembly: VerboseTraceAspect(AspectPriority = 1, AttributeExclude = true, AttributeTargetMembers = "regex:get_.*|set_.*")]

[assembly: VerboseTraceAspect(AspectPriority = 2, AttributeTargetTypes = "TestProject.*", 
    AttributeTargetTypeAttributes = MulticastAttributes.Public, 
    AttributeTargetMemberAttributes = MulticastAttributes.Public, 
    AttributeTargetElements = MulticastTargets.Method)] 

Upvotes: 2

Views: 1400

Answers (1)

AlexD
AlexD

Reputation: 5101

You need to correct the target types expression in the first line where you remove your trace attribute from TestProject.Logging.* and TestProject.Tracing.*. If you want to specify several options separated with pipeline, then you should use regular expressions syntax.

You also need to use AttributePriority instead of AspectPriority property. The attribute multicasting is performed before the AspectPriority has any effect. It's used later to determine the order in which your aspects will execute.

The "exclude" attributes must have a higher priority value (higher values are processed after lower values).

[assembly: VerboseTraceAspect(
    AttributePriority = 1,
    AttributeExclude = true,
    AttributeTargetTypes = @"regex:TestProject\.Logging\..+|TestProject\.Tracing\..+")]

[assembly: VerboseTraceAspect(
    AttributePriority = 2,
    AttributeExclude = true,
    AttributeTargetMembers = "regex:get_.*|set_.*")]

[assembly: VerboseTraceAspect(
    AttributePriority = 0,
    AttributeTargetTypes = "TestProject.*",
    AttributeTargetTypeAttributes = MulticastAttributes.Public,
    AttributeTargetMemberAttributes = MulticastAttributes.Public,
    AttributeTargetElements = MulticastTargets.Method)] 

Upvotes: 2

Related Questions