【问题标题】:Disable nlog logging in PostSharp 5.x在 PostSharp 5.x 中禁用 nlog 日志记录
【发布时间】:2017-08-17 11:32:38
【问题描述】:

可以使用LogManager.DisableLogging();禁用登录NLog

用 PostSharp+NLog 组合怎么办?

【问题讨论】:

  • 为什么不打电话给LogManager.DisableLogging()

标签: nlog postsharp


【解决方案1】:

http://doc.postsharp.net/log-enabling 详细介绍了启用和禁用 PostSharp 日志记录。

简而言之,您有两种选择:

1) 例如,通过 PostSharp API 为特定的日志记录角色和命名空间禁用日志记录

LoggingServices.DefaultBackend.GetSource(LoggingRoles.Tracing, MyCompany.BusinessLayer").SetLevel(LogLevel.Debug);

2) 使用日志记录后端 API。

在使用文档中描述的这种方法之前,您可能需要进行一些设置。

但是,NLog 的LogManager.DisableLogging(); 可以被 PostSharp 识别,因此您可以使用它来完全禁用日志记录,包括 PostSharp 日志记录。

【讨论】:

  • 在下面的代码中,LogManager.DisableLogging() 没有任何影响,我仍然得到方法进入/退出跟踪。 static void Main(string[] args) { LoggingServices.DefaultBackend = new PostSharp.Patterns.Diagnostics.Backends.Console.ConsoleLoggingBackend(); NLog.LogManager.DisableLogging(); f(); } [Log] public static void f() { } }
  • 那是因为您使用的是ConsoleLoggingBackend 而不是NLogLoggingBackend。要使 PostSharp Logging 使用 NLog,请将包 PostSharp.Patterns.DiagnosticsPostSharp.Patterns.Diagnostics.NLog 添加到您的项目中,根据 NLog 文档初始化 NLog 并设置 PostSharp通过LoggingServices.DefaultBackend = new PostSharp.Patterns.Diagnostics.Backends.Console.NLogLoggingBackend(); 使用NLog。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多