PostSharp-使用AttributeExclude排除方法不起作用 [英] PostSharp - excluding a method using AttributeExclude doesn't work

查看:73
本文介绍了PostSharp-使用AttributeExclude排除方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个项目,正在使用PostSharp启用日志记录.效果很好.但是,有两种方法在非常紧密的循环中运行,其中日志记录的开销实际上加起来相当可观.我正在尝试找出从日志代码中排除它们的最佳方法.

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(...)

但是,当我执行此操作并使用探查器运行它时,我仍然看到在SomeMethod()上正在执行的日志代码.我根据SO和PostSharp论坛上的帖子尝试了许多不同的迭代,但我总是得到相同的结果.

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.

推荐答案

我发现我的SomeMethod()例程中有一个LINQ查询,表明编译器正在将其转换为单独的方法.PostSharp还使用Log属性修饰了此编译器生成的方法.在事件探查器中查看该事件尚不清楚,但是在使用Resharper深入研究生成的代码之后,我就知道了.

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.

这篇关于PostSharp-使用AttributeExclude排除方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆