简体   繁体   English

如何将外部包日志添加到 Nlog 中的当前项目日志文件/规则

[英]How to add external packages logs to current project log files/rules in Nlog

I have some rules, that logging their projects我有一些规则,记录他们的项目

{
    "logger": "Alpha.*",
    "minLevel": "${configsetting:item=Alpha.LogLevel}",
    "ruleName": "Alpha",
    "writeTo": "fileTarget, consoleTarget"
  },
  {
    "logger": "Beta.*",
    "minLevel": "${configsetting:item=Beta.LogLevel}",
    "ruleName": "Beta",
    "writeTo": "fileTarget, consoleTarget"
  },

Now I add few packages to my solution (like AB.Common.Exception ), and I want to add their logs for the log of project.现在我向我的解决方案添加了几个包(比如AB.Common.Exception ),我想将它们的日志添加到项目日志中。

For example, if package was used by Alpha , log from AB might be added to "ruleName": "Alpha" , but not to "Beta" .例如,如果 package 被Alpha使用,来自AB的日志可能会添加到"ruleName": "Alpha" ,但不会添加到"Beta"

Is there any way to do this?有什么办法吗?

Regards.问候。

Updated更新

Ok, I found possible answer with rule in end好的,我最终找到了可能的规则答案

{
    "logger": "Alpha.*",
    "minLevel": "${configsetting:item=Alpha.LogLevel}",
    "ruleName": "Alpha",
    "writeTo": "fileTarget, consoleTarget"
  },
  {
    "logger": "Beta.*",
    "minLevel": "${configsetting:item=Beta.LogLevel}",
    "ruleName": "Beta",
    "writeTo": "fileTarget, consoleTarget"
  },
  {
    "logger": "*",
    "minLevel": "Trace",
    "ruleName": "AB",
    "writeTo": "fileTarget, consoleTarget"
  }

But I want to set logLevel from ${configsetting:item=Alpha.LogLevel} or ${configsetting:item=Beta.LogLevel}但我想从${configsetting:item=Alpha.LogLevel}${configsetting:item=Beta.LogLevel}设置 logLevel

Ok, my answer now looks like that好的,我的答案现在看起来像这样

{
    "logger": "Alpha.*",
    "minLevel": "${configsetting:item=Alpha.LogLevel}",
    "ruleName": "Alpha",
    "writeTo": "fileTarget, consoleTarget"
  },
  {
    "logger": "Beta.*",
    "minLevel": "${configsetting:item=Beta.LogLevel}",
    "ruleName": "Beta",
    "writeTo": "fileTarget, consoleTarget"
  },
  {
    "logger": "*",
    "minLevel": "${configsetting:item=Alpha.LogLevel}",
    "ruleName": "ABtoAlpha",
    "writeTo": "fileTarget, consoleTarget"
  },
  {
    "logger": "*",
    "minLevel": "${configsetting:item=Beta.LogLevel}",
    "ruleName": "ABtoBeta",
    "writeTo": "fileTarget, consoleTarget"
  }

I think it's not so beautiful I hoped, but it works我认为它不是我希望的那么漂亮,但它有效

Maybe you just need to add "final": true , so it doesn't reach the last catch-all-rule:也许你只需要添加"final": true ,这样它就不会达到最后一个包罗万象的规则:

"rules": [
  {
    "logger": "Alpha.*",
    "minLevel": "${configsetting:item=Alpha.LogLevel}",
    "ruleName": "Alpha",
    "writeTo": "fileTarget, consoleTarget",
    "final": true
  },
  {
    "logger": "Beta.*",
    "minLevel": "${configsetting:item=Beta.LogLevel}",
    "ruleName": "Beta",
    "writeTo": "fileTarget, consoleTarget",
    "final": true
  },
  {
    "logger": "*",
    "minLevel": "Trace",
    "ruleName": "AB",
    "writeTo": "fileTarget, consoleTarget"
  }
]

See also: https://github.com/NLog/NLog/wiki/Configuration-file#rules另见: https://github.com/NLog/NLog/wiki/Configuration-file#rules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM