c# - 如何从基于 OnMethodBoundaryAspect 的日志记录中排除?

标签 c# c#-4.0 postsharp aop

我有这个记录器:

[Serializable]
[AttributeUsage(AttributeTargets.All)]
public class MethodsInterceptAspect : OnMethodBoundaryAspect {
    public override void OnEntry(MethodExecutionArgs args) { Logger.LogMethodEntry(args.Method, DateTime.Now); }
    public override void OnExit(MethodExecutionArgs args) { Logger.LogMethodExit(args.Method, DateTime.Now); }
}

但是有一个密集型函数(许多嵌套循环,对性能至关重要)也会使日志膨胀。我如何排除它及其所有子例程?

最佳答案

您可以使用方面的 AttributeExclude=true 属性来执行此操作。可以在程序集级别应用排除

[assembly: Trace("Business", AttributeTargetTypes="BusinessLayer.*", AttributePriority = 1)]
[assembly: Trace(AttributeTargetMembers="Dispose", AttributeExclude = true, AttributePriority = 2)]

或按方法

[assembly: Trace("Business", AttributeTargetTypes="BusinessLayer.*")]
namespace BusinessLayer
{
  public class Process : IDisposable
  {
   public Customer Create(string value) { ... }
   public void Delete(long id) { ... }

   [Trace(AttributeExclude=true)]
   public void Dispose() { ... }
  }
}

A more complete answer can be found here

关于c# - 如何从基于 OnMethodBoundaryAspect 的日志记录中排除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15501707/

相关文章:

c# - 在运行时打开/关闭 Postsharp 跟踪?

visual-studio-2017 - Visual Studio 2017不支持代码契约,如何使用PostSharp实现它?

c# - 从任何应用程序调用默认命令行解析方法

unicode - 是否可以将语言特定字符转换为 UTF8 中的拉丁字符

c# - 如何将导航属性名称更改为有意义的名称

c#-4.0 - 如何使用 Office.Interop.Excel 进行交替行着色?

.net - 从 MS 示例创建 Windows 服务时出现错误 "Could not find xxxx.Program specified for main method"

c# - 使用 PostSharp 1.5 实现 INotifyPropertyChanged

c# - 无法将类型 System.Data.Entity.Core.Objects.ObjectResult 隐式转换为 System.Data.Objects.ObjectResult

c# - 如何在 Win8.1x64 中配置 EmguCV 和 Visual Studio Express 2010