ILogger.LogError 未记录异常详细信息

Man*_*ngh 6 azure azure-application-insights azure-functions

我有一个函数应用程序(2.0 版)并试图记录异常的堆栈跟踪。

     try
        {
            processedOrder = await orderProcessingService.ProcessOrder(order);
        }
        catch (Exception ex)
        {
            log.LogError(ex, $"Error processing order: {order.Id}");
        }
Run Code Online (Sandbox Code Playgroud)

这仅记录 App Insights 中的消息,但不记录传递的异常对象。

我这样做对吗?

如果我这样做, log.LogError(ex, $"Error processing order: {order.Id}", ex)那么我确实会看到异常消息,但不会看到堆栈跟踪。

小智 2

除了记录错误之外,您还需要使用遥测客户端显式跟踪异常以获取异常详细信息。我在 Web API 2.0 中实现了 ExceptionLogger,以使用跟踪异常方法将异常详细信息发送到 app-insights。

var telemetry = new TelemetryClient();
    ...
    try
    { ...
    }
    catch (Exception ex)
    {       
       telemetry.TrackException(ex, properties, measurements);
    }
Run Code Online (Sandbox Code Playgroud)

  • 我不确定这是否正确。通过 ILogger 记录异常预计会在应用程序洞察中创建 ExceptionTelemetry。情况确实如此(https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#what-application-insights-telemetry-type-is- Produced-from-ilogger-logs-或-where-can-i-see-ilogger-logs-in-application-insights) 对于所有非函数。从azure-web-job代码来看,函数也应该如此:https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ ApplicationInsightsLogger.cs#L105 (5认同)