dev*_*r82 0 c# nlog asp.net-core
我正在使用 ASP.NET Core 记录器和 NLog。当我记录错误/严重异常时,会记录消息,但不会记录异常。
try
{
// my code throws an exception
}
catch (Exception ex)
{
_logger.LogError($"There was an error", ex);
return null;
}
Run Code Online (Sandbox Code Playgroud)
我的 NLog 配置如下所示:
<target xsi:type="File" name="ApiLogger" fileName="Logs\api-${shortdate}.log"
layout="${longdate} - ${uppercase:${level}} - ${threadid} - ${logger} - ${message} ${exception:innerFormat=Message,StackTrace}" />
Run Code Online (Sandbox Code Playgroud)
文件中记录的内容There was an error没有实际的异常和堆栈跟踪。
小智 7
ASP.NET Core 记录器所需的正确语法是
ILogger.LogError(Exception exception, string message);
Run Code Online (Sandbox Code Playgroud)
写的代码是使用扩展方法
ILogger.LogError(string message, params object[] args);
Run Code Online (Sandbox Code Playgroud)
这意味着您应该尝试使用它:
try
{
// my code throws an exception
}
catch (Exception ex)
{
_logger.LogError(ex, "There was an error");
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1853 次 |
| 最近记录: |