【问题标题】:ASP.NET Core 3 - Logging with NLog does not log exceptionsASP.NET Core 3 - 使用 NLog 进行日志记录不会记录异常
【发布时间】:2019-12-27 02:10:25
【问题描述】:

我正在使用 ASP.NET Core 记录器和 NLog。当我记录带有异常的错误/严重时,会记录消息,但不会记录异常。

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

我的 NLog 配置如下所示:

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

文件中记录的是There was an error,没有实际的异常和堆栈跟踪。

【问题讨论】:

    标签: c# asp.net-core nlog


    【解决方案1】:

    您想要的 ASP.NET Core 记录器的正确语法是

    ILogger.LogError(Exception exception, string message);
    

    编写的代码是使用扩展方法

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

    这意味着您应该尝试使用它:

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

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      相关资源
      最近更新 更多