首页 > 解决方案 > NLOG 不在服务器上记录未处理的异常

问题描述

我遇到以下行为:

  1. 在开发(本地)中记录异常没有问题,所有类型的作品。

  2. 当我登录远程服务器(通过远程桌面连接)并在服务器内打开一个 chrome 并创建异常时,也没有问题,所有类型的日志记录都有效。

  3. 如果我使用笔记本电脑和 Chrome 浏览器打开服务器上托管的同一个网站:

控制器/动作

{
   // log record gets created for the following line
   logger.Log(LogLevel.Fatal, "Sample informational message"); 

   // log record not created. However this record get created in item 1 and 2 case above
   int a = int.Parse("A");  
}

全球.asax

 protected void Application_Error(object sender, EventArgs e)
 {
        Logger logger = LogManager.GetCurrentClassLogger();
        logger.Fatal(Server.GetLastError());
        Server.ClearError();
 }

NLog.config

<?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"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  autoReload="true"
  throwExceptions="false"
  internalLogLevel="Off"
  internalLogFile="D:\...\logs\nlog-internal.log">

  <!--Define Various Log Targets like files, database or asp.net trace files-->
  <targets>
    <target name="console" xsi:type="ColoredConsole" layout="${message}" />
    <!--Write logs to File  where we want to write error logs-->
    <target name="file"              
        archiveFileName="logs/archives/app-log.{#}.txt" 
        archiveEvery="Day" 
        archiveNumbering="Rolling" 
        maxArchiveFiles="7" 
        xsi:type="File" 
        fileName="D:\...\logs\ErrorLogFile.log" 
        layout="--------------------- ${level} (${longdate}) ${machinename} --------------------
        ${newline}      
        ${newline}
        Additional Info  : ${message}${newline}
        Exception Type   : ${exception:format=Type}${newline}
        Exception Message: ${exception:format=Message}${newline}
        Stack Trace      : ${exception:format=Stack Trace}${newline}

        ">
    </target>

  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="file" />

  </rules>
</nlog>

internalLogLevel="Debug" 输出:

2018-09-20 19:26:10.4825 Debug ScanAssembly('NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2018-09-20 19:26:10.5275 Debug Search for auto loading files, location: C:\inetpub\wwwroot\Test
2018-09-20 19:26:10.5275 Debug Start auto loading, location: C:\inetpub\wwwroot\Test
2018-09-20 19:26:10.5345 Debug Auto loading done
2018-09-20 19:26:10.5345 Info Message Template Auto Format enabled
2018-09-20 19:26:10.5345 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2018-09-20 19:26:10.5345 Debug Setting 'ColoredConsoleTarget.name' to 'console'
2018-09-20 19:26:10.5345 Debug Setting 'ColoredConsoleTarget.layout' to '${message}'
2018-09-20 19:26:10.5345 Info Adding target ColoredConsole Target[console]
2018-09-20 19:26:10.5345 Debug Registered target console: NLog.Targets.ColoredConsoleTarget
2018-09-20 19:26:10.5525 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.name' to 'file'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.archiveFileName' to 'logs/archives/app-log.{#}.txt'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.archiveEvery' to 'Day'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.archiveNumbering' to 'Rolling'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.maxArchiveFiles' to '7'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.fileName' to 'C:\temp\logs\ErrorLogFile.log'
2018-09-20 19:26:10.5525 Debug Setting 'FileTarget.layout' to '--------------------- ${level} (${longdate}) ${machinename} -------------------- ${newline} ${newline} Additional Info : ${message}${newline} Exception Type : ${exception:format=Type}${newline} Exception Message: ${exception:format=Message}${newline} Stack Trace : ${exception:format=Stack Trace}${newline} '
2018-09-20 19:26:10.5795 Debug Setting 'ExceptionLayoutRenderer.format' to 'Type'
2018-09-20 19:26:10.5815 Debug Setting 'ExceptionLayoutRenderer.format' to 'Message'
2018-09-20 19:26:10.5815 Debug Setting 'ExceptionLayoutRenderer.format' to 'Stack Trace'
2018-09-20 19:26:10.5815 Info Adding target File Target[file]
2018-09-20 19:26:10.5815 Debug Registered target file: NLog.Targets.FileTarget
2018-09-20 19:26:10.5815 Info Configured from an XML element in C:\inetpub\wwwroot\Test\NLog.config...
2018-09-20 19:26:10.5815 Debug Unused target checking is started... Rule Count: 1, Target Count: 2
2018-09-20 19:26:10.5815 Warn Unused target detected. Add a rule for this target to the configuration. TargetName: console
2018-09-20 19:26:10.5815 Debug Unused target checking is completed. Total Rule Count: 1, Total Target Count: 2, Unused Target Count: 1
2018-09-20 19:26:10.5815 Debug --- NLog configuration dump ---
2018-09-20 19:26:10.5815 Debug Targets:
2018-09-20 19:26:10.5815 Debug ColoredConsole Target[console]
2018-09-20 19:26:10.5815 Debug File Target[file]
2018-09-20 19:26:10.5815 Debug Rules:
2018-09-20 19:26:10.5815 Debug logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] appendTo: [ file ]
2018-09-20 19:26:10.5815 Debug --- End of NLog configuration dump ---
2018-09-20 19:26:10.5815 Info Found 23 configuration items
2018-09-20 19:26:10.6105 Debug Watching path 'C:\inetpub\wwwroot\Test' filter 'NLog.config' for changes.
2018-09-20 19:26:10.6115 Info Configuration initialized.
2018-09-20 19:26:10.6115 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.5.7.7958. Product version: 4.5.7.
2018-09-20 19:26:10.6115 Debug Targets for Data.Repositories.CommonRepository by level:
2018-09-20 19:26:10.6115 Debug Trace => file
2018-09-20 19:26:10.6115 Debug Debug => file
2018-09-20 19:26:10.6115 Debug Info => file
2018-09-20 19:26:10.6115 Debug Warn => file
2018-09-20 19:26:10.6115 Debug Error => file
2018-09-20 19:26:10.6115 Debug Fatal => file
2018-09-20 19:26:10.6405 Debug Targets for Emailer.EmailManager by level:
2018-09-20 19:26:10.6405 Debug Trace => file
2018-09-20 19:26:10.6405 Debug Debug => file
2018-09-20 19:26:10.6405 Debug Info => file
2018-09-20 19:26:10.6405 Debug Warn => file
2018-09-20 19:26:10.6405 Debug Error => file
2018-09-20 19:26:10.6405 Debug Fatal => file
2018-09-20 19:26:10.6435 Debug Targets for UI.Controllers.BaseController by level:
2018-09-20 19:26:10.6435 Debug Trace => file
2018-09-20 19:26:10.6435 Debug Debug => file
2018-09-20 19:26:10.6435 Debug Info => file
2018-09-20 19:26:10.6435 Debug Warn => file
2018-09-20 19:26:10.6435 Debug Error => file
2018-09-20 19:26:10.6435 Debug Fatal => file
2018-09-20 19:26:10.7735 Debug FileTarget(Name=file): Process file 'C:\temp\logs\ErrorLogFile.log' on startup
2018-09-20 19:26:10.7735 Debug Creating file appender: C:\temp\logs\ErrorLogFile.log

MVC 5 项目。.Net 4.5.2。NLog 版本 4.5.7

我也在项目中使用 SignalR。日志文件夹具有 IIS_IUSRS 的修改权限。

任何帮助表示赞赏。

标签: nlog

解决方案


In my case, It turned out Application_Error do not get called in server environment. implementing OnException method and logging in the method solved the issue.

I post my answer https://github.com/NLog/NLog/issues/2924


推荐阅读