简体   繁体   English

ASP.NET Core 3 - 使用 NLog 记录不会记录异常

[英]ASP.NET Core 3 - Logging with NLog does not log exceptions

I'm using the ASP.NET Core logger along with NLog.我正在使用 ASP.NET 核心记录器和 NLog。 When I log an error/critical with an exception, the message is logged but the exception doesn't.当我记录带有异常的错误/严重时,会记录消息,但不会记录异常。

try
{
    // my code throws an exception
}
catch (Exception ex)
{
    _logger.LogError($"There was an error", ex);
    return null;
}

My NLog configuration looks like this:我的 NLog 配置如下所示:

<target xsi:type="File" name="ApiLogger" fileName="Logs\api-${shortdate}.log"
        layout="${longdate} - ${uppercase:${level}} - ${threadid} - ${logger} - ${message} ${exception:innerFormat=Message,StackTrace}" />

Whats logged in the file is There was an error without the actual exception and stacktrace.文件中记录的是没有实际异常和堆栈跟踪There was an error

The correct syntax that you want for an ASP.NET Core logger is您想要的 ASP.NET 核心记录器的正确语法是

ILogger.LogError(Exception exception, string message);

The code written is using the extension method编写的代码是使用扩展方法

ILogger.LogError(string message, params object[] args);

This means you should try using this instead:这意味着您应该尝试使用它:

try
{
    // my code throws an exception
}
catch (Exception ex)
{
    _logger.LogError(ex, "There was an error");
    return null;
}

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

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