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

查看:35
本文介绍了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天全站免登陆