Làm cách nào để loại trừ khỏi ghi nhật ký dựa trên OnMethodBoundaryAspect? (How to exclude from OnMethodBoundaryAspect-based logging?)


問題描述

Làm cách nào để loại trừ khỏi ghi nhật ký dựa trên OnMethodBoundaryAspect? (How to exclude from OnMethodBoundaryAspect‑based logging?)

I have this logger:

[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); }
}

But there's an intensive function (many nested loops, performance‑critical) that also bloats the log. How do I exclude it and all its subroutines ?


參考解法

方法 1:

You can do this with the AttributeExclude=true property of the aspect. The exclusion can be applied at the assembly level

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

or per method

[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

(by Tarqujck)

參考文件

  1. How to exclude from OnMethodBoundaryAspect‑based logging? (CC BY‑SA 3.0/4.0)

#postsharp #aop #c#-4.0 #C#






相關問題

拋出自定義異常並使用 Postsharp 捕獲它們 (Throw an custom exception and catch them with Postsharp)

Làm cách nào để loại trừ khỏi ghi nhật ký dựa trên OnMethodBoundaryAspect? (How to exclude from OnMethodBoundaryAspect-based logging?)

在屬性中傳遞動態參數 (PostSharp) (Passing dynamic parameters in attributes (PostSharp))

PostSharp 3.0 中 IAspectProvider 的重大變化? (Breaking change of IAspectProvider in PostSharp 3.0?)

將方面應用於其他程序集類方法調用 (apply an aspect to other assembly class methods calls)

Postsharp 和 log4net 和 log4postsharp (Postsharp and log4net and log4postsharp)

Postsharp - 獲取調用程序集? (Postsharp - Get Calling Assembly?)

PostSharp 對象映射器 (PostSharp for an object mapper)

PostSharp - 自動化事件訂閱和集合添加 (PostSharp - automate event subscription and collection addition)

PostSharp:在目標構造函數之後初始化實例範圍的方面 (PostSharp: initialize instance-scoped aspect after target constructors)

使用 PostSharp 1.0 的 ClickOnce 應用程序似乎需要 GAC 中的 1.5 程序集 (ClickOnce application that uses PostSharp 1.0 seems to require 1.5 assemblies in GAC)

更改用戶控件 DependencyProperty 時屬性重置 (Property resets when changing Usercontrols DependencyProperty)







留言討論