问题
We have a project where we're using PostSharp to enable logging. Works great. However, there are a couple of methods that are run in very tight loops, where the overhead of logging really adds up to a considerable amount. I'm trying to figure out the best way to exclude them from the logging code.
From what I've read, this approach should work
AssemblyInfo.cs
// turn on logging for all methods in all classes
[assembly: Log(LogType.Debug)]
Code.cs
// exclude this specific method
[Log(AttributeExclude=true)]
private void SomeMethod(...)
However, when I do this and run it with a profiler, I still see the log code being executed on SomeMethod(). I've tried many different iterations of this based on posts on SO and the PostSharp forums, and I always get the same result.
It feels like this should be really simple, and I'm just missing something blatantly obvious.
回答1:
I turns out my SomeMethod() routine had a LINQ query in it that the compiler was turning into an separate method. This compiler generated method was also being decorated by PostSharp with the Log attribute. Looking at this in the profiler it wasn't clear what was happening, but after using Resharper to dig through the generated code, I figured it out.
来源:https://stackoverflow.com/questions/4857245/postsharp-excluding-a-method-using-attributeexclude-doesnt-work