Reputation: 2268
I would like to do the following:
Suppose I have a logging aspect implemented in a class named LoggingAspect. I would like to configure an entire assembly to log at some log level using the multicast attribute facility. Now suppose I want a specific class in an assembly to have a slightly different log level I would like to decorate the entire class or a specific method in that class and as a result to get a log message with the class specified or method specified level.
My questions are:
Thanks
Upvotes: 0
Views: 126
Reputation: 2268
After reading the PostSharp documentation it seems that the solution is to annotate the method with the aspect attribute and then Specify AttributeReplace = true. Here is a complete solution to my question.
Let's suppose I defined a logging aspect in a class named LoggingAspectAttribute that gets a TraceLevel on the constructor. On AssemblyInfo.cs I add the following definition:
[assembly: TracingAspect(TraceLevel.Info,
AttributeTargetTypes = "PostSharp2.*",
AttributeTargetTypeAttributes = MulticastAttributes.Public,
AttributeTargetMemberAttributes = MulticastAttributes.Private | MulticastAttributes.Public)]
PostSharp2 is just the name of the assembly I used to test my solution. This definition causes all my traces to be with the Information TraceLevel.
To override this definition I do the following:
[TracingAspect(TraceLevel.Warning, AttributeReplace = true)]
private static void Bar()
{
Console.WriteLine("Inside Bar");
}
This causes the trace message for Bar to be with the Warning TraceLevel and all other messages remain with the Information trace level.
Now, if I will omit the AttributeReplace property and leave the attribute annotation as follows:
[TracingAspect(TraceLevel.Warning)]
private static void Bar()
{
Console.WriteLine("Inside Bar");
}
I will see 2 trace messages from Bar. One with an Information level and another with a warning level.
Hope that helps somebody.
Upvotes: 0