Catch块的入库日志记录语句

时间:2020-02-14 23:07:29

标签: asp.net-core log4net core

我正在使用.Core 3.1应用程序,该应用程序将具有常规的try / catch块---它们的形成类似于:

Logger.LogWarning(ex, "Failed to load product {ProductId}", 

会有很多这样的块-记录捕获的所有内容几乎都是相同的:一个字符串消息,每个块都是唯一的,加上异常消息。

因此,如果每个catch块都在调用记录器,为什么需要显式调用?将呼叫“接收”到记录器并传递要通过约定建立的日志消息的扩展点是什么。

因此请考虑以下代码块:

protected override async Task OnParametersSetAsync()
{
    var _logMessage = "OPSA calls GetProductByIdAsync with: " + ProductId;
    try
    {
        _loadFailed = false;
        _details = await GetProductByIdAsync(ProductId);
    }
    catch (Exception ex)
    {
        _loadFailed = true;
        //app should implicitly call:
        // Logger.LogWarning(ex, _logMessage);
        // 
       // Shouldn't I expect to find a special something somewhere
       // to override the default behavior and inject that logger call?
    }

谢谢 }

0 个答案:

没有答案
相关问题