ASP.NET Core 3 - 使用 NLog 记录不记录异常
ASP.NET Core 3 - Logging with NLog does not log exceptions
我正在使用 ASP.NET 核心记录器和 NLog。当我记录 error/critical 异常时,会记录消息但不会记录异常。
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
没有实际异常和堆栈跟踪。
ASP.NET 核心记录器所需的正确语法是
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;
}
我正在使用 ASP.NET 核心记录器和 NLog。当我记录 error/critical 异常时,会记录消息但不会记录异常。
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
没有实际异常和堆栈跟踪。
ASP.NET 核心记录器所需的正确语法是
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;
}