开发者

PostSharp - excluding a method using AttributeExclude doesn't work

开发者 https://www.devze.com 2023-02-07 23:53 出处:网络
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

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号