【问题标题】:NLog Refuse to Throw ExceptionNLog 拒绝抛出异常
【发布时间】:2012-09-11 22:40:34
【问题描述】:

亲爱的开发者你好,

我在使用 NLog 时遇到了一个奇怪的问题,
我发生了崩溃,但在日志中找不到用户活动的踪迹。

我认为,日志记录不起作用...
尝试搜索权限问题等,但似乎一切正常

我想调试我的日志记录以防出现问题,所以我创建了以下
告诉用户它是否未能创建任何东西的代码:

public static class Log
{
    static Log()
    {
        try
        {
            AppLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.AppLogger");
            ChangeLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.ChangeLogger");
            DRCLogger = LogManager.GetLogger("Megatec.EngineeringMatrix.DRCLogger");

            if((AppLogger == null) || (ChangeLogger == null) || (DRCLogger == null))
                throw new NLogConfigurationException("Configuration does not specify correct loggers");
            writeStartupLogEntry();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), @"Failed to load `Megatec.EngineeringMatrix` Loggers.");
            throw;
        }

    }

    public static readonly Logger AppLogger;
    public static readonly Logger ChangeLogger;
    public static readonly Logger DRCLogger;

使用以下配置文件:

  <?xml version="1.0" encoding="utf-8" ?>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
    <targets>
      <target xsi:type="File" name="AppLogFile" createDirs="true" fileName="c:/log?s/${processname}/${logger}_${shortdate}.log"
              layout=">> ${time} ${uppercase:${level}} ${callsite} ${message} ${exception:format=tostring}" />

      <target xsi:type="File" name="ChangeLogFile" createDirs="true" fileName="c:/log?s/${processname}/${logger}_${shortdate}.log"
              layout=">> ${date} ${message} ${exception:format=tostring}" />

      <target xsi:type="File" name="DRCLogFile" createDirs="true" fileName="c:/logs/${processname}/${logger}_${shortdate}.log"
              layout=">> ${date} ${message} ${exception:format=tostring}" />
    </targets>

    <rules>
      <logger name="Megatec.EngineeringMatrix.AppLogger" minlevel="Trace" writeTo="AppLogFile" />
      <logger name="Megatec.EngineeringMatrix.ChangeLogger" minlevel="Trace" writeTo="ChangeLogFile" />
      <logger name="Megatec.EngineeringMatrix.DRCLogger" minlevel="Trace" writeTo="DRCLogFile" />
    </rules>
  </nlog>

显然我写了一个 BAAD 配置,因为无法创建目录 c:\lo?gs

但我还是没有收到消息
MessageBox.Show(ex.ToString(), @"Failed to load 'Megatec.EngineeringMatrix'

甚至AppLogger.Info() 也会跳过异常...

我没有写任何记录,但应用程序不知道...

在调试中,我可以捕捉到异常,并看到它是由NLog处理的,

如何从我的代码中捕获它?

【问题讨论】:

  • 您是否尝试过此处的故障排除步骤:nlog-project.org/wiki/…?默认情况下,日志记录异常被吞没。
  • 是的,如您所见,我添加了必要的配置参数.. 但仍然。我可以在 internalLog 中看到,但没有传递给应用程序。

标签: c# debugging exception nlog throw


【解决方案1】:

这是一个老问题,但最近有一些变化。

过去 NLog “吃掉”了一些异常。例如在AsyncWrapper 中(或在&lt;target&gt; 上使用属性async

这已在 NLog 4.3 中修复:

一致地处理异常(行为变化) 异常的记录和抛出以前是不一致的。并非所有这些都记录到内部记录器中,有时它们会丢失。例如,异步包装器(或异步属性)在没有正确重新抛出的情况下捕获所有异常。

这很糟糕,因为有时不清楚为什么 NLog 不工作(我自己弄了很多次)。这已在 NLog 4.3 中修复!

所有异常都记录到内部记录器 在所有情况下都将遵守“throwExceptions”选项 建议:在生产环境中禁用 throwExceptions! (这是默认值)

http://nlog-project.org/2016/04/16/nlog-4-3-has-been-released.html

【讨论】:

    猜你喜欢
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2020-10-28
    • 2012-10-19
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多