PostSharp - excluding a method using AttributeExclude doesn't work

こ雲淡風輕ζ 提交于 2021-01-29 01:42:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!