簡體   English   中英

NLog拒絕引發異常

[英]NLog Refuse to Throw Exception

您好親愛的開發人員,

我在使用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已“吃掉”了一些例外。 例如在AsyncWrapper (或在<target>上使用屬性async

NLog 4.3中已修復此問題:

異常的一致處理(行為更改)以前,異常的日志記錄和引發是不一致的。 並非所有日志都記錄到內部記錄器中,有時它們會丟失。 例如,異步包裝器(或異步屬性)在沒有適當重新拋出的情況下捕獲了所有異常。

這很糟糕,因為有時不清楚為什么NLog無法正常工作(我自己多次獲取了它)。 此問題已在NLog 4.3中修復!

所有異常都記錄到內部記錄器中。在所有情況下都將使用“ throwExceptions”選項。建議:在生產環境中禁用throwException! (這是默認設置)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM