【问题标题】:How to count error messages in nlog from code如何从代码中计算 nlog 中的错误消息
【发布时间】:2019-08-20 05:09:27
【问题描述】:

我正在使用 NLog 库登录我的应用程序。有一项任务要从日志文件中的代码错误和致命消息中计数。有内置的方法吗?我尝试申请{$counter},但我不能只将它用于错误和致命消息而不打印到日志文件。

【问题讨论】:

  • 从什么时候开始计算?申请开始?应该在何时何地打印计数?
  • 是的,因为应用程序启动。我正在将应用程序中当前的日志记录方法替换为 NLog。它具有 ErrorCount 属性。属性值不会打印到文件中。它用于自动测试,理论上可以传递给另一个收集统计信息的应用程序。

标签: .net nlog


【解决方案1】:

好奇这个数字应该用来做什么,但一种方法可能是MethodCall-target:

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <target name="m" xsi:type="MethodCall" className="SomeNamespace.MyClass, MyAssembly" methodName="LogMethod">
        <parameter layout="${level}" />
    </target>

    <rules>
        <logger name="*" minlevel="Error" writeTo="m" />
    </rules>
</nlog>

然后是“MyAssembly”-project 中的以下代码:

namespace SomeNamespace
{
    using System;

    public class MyClass
    {
        static int ErrorCounter;

        public static void LogMethod(string level)
        {
            if (level == LogLevel.Error.ToString())
               ++ErrorCounter;
        }
    }
}

另见https://github.com/NLog/NLog/wiki/MethodCall-target

【讨论】:

  • 非常感谢!我认为它适合我的任务。
猜你喜欢
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多