首页 > 解决方案 > 我的 Azure 函数的异常在 Application Insights 中记录了 3 次

问题描述

我已经创建了一个 Blob Trigger Azure Function,我希望能够在出现异常时创建邮件警报,并且在邮件警报中发送的信息中,应该有发生异常的文件的名称。

我注意到异常会自动记录在 Azure 中,但我发现无法自定义消息或异常发送的信息。所以我决定在我的 Function App 中注入遥测服务,并将文件名添加为自定义属性,如下面的代码所示:

public class Function1
{
    private readonly IGremlinService _gremlinService;
    private readonly TelemetryClient _telemetryClient;
    public Function1(IGremlinService gremlinService, TelemetryConfiguration telemetryConfiguration)
    {
        this._gremlinService = gremlinService;
        this._telemetryClient = new TelemetryClient(telemetryConfiguration);
    }

    [FunctionName(nameof(Function1))]
    public async Task Run([BlobTrigger("files/{directory}/{name}.00.pdf", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger logger)
    {
        try
        {
             //some code not related to the issue
        }
        catch (Exception e)
        {
            var properties = new Dictionary<string, string>
                {{"Filename", name}};
            _telemetryClient.TrackException(e, properties);
            if (e is ResponseException)
            {
                ResponseException re = (ResponseException) e;

                var statusCode = (long) re.StatusAttributes["x-ms-status-code"];
               
                _telemetryClient.TrackTrace("Error on file " + name + ". Status code: " + statusCode + " " + re.StackTrace, SeverityLevel.Error, properties);
            }
            else
            {
                _telemetryClient.TrackTrace("Error on file " + name, SeverityLevel.Error, properties);
            }
            throw;
        }
    }
}
  }   

但我仍然无法自定义消息以向用户提供附加信息。我知道我可以改为发送有关跟踪消息的警报,并以这种方式发送自定义消息,这就是我目前正在做的事情,但我会发现发送异常警报更简洁。

我的第二个问题是,我的异常仍然在遥测服务记录的基础上自动记录,并且由于某种我无法理解的原因,它们被记录了两次,正如您在 Application Insights 下面的屏幕截图中看到的那样:

在此处输入图像描述

有没有办法可以关闭异常的自动记录?或者有没有办法自定义这些我不知道的异常消息,而不是使用遥测服务?

标签: c#exceptionazure-functionsazure-application-insightstelemetry

解决方案


我相信,由于以下原因,正在记录 3 个异常:

  1. IGremlinService正在记录的抛出异常的实现服务。
  2. 您正在通过登录_telemetryClient.TrackException(e, properties);
  3. Azure 基础结构正在处理何时throw被调用。

现在来回答你的问题

我发现无法自定义消息或异常发送的信息

我建议您使用ILoggerLogException 并使用BeginScope在此处阅读)定义范围属性,这些属性将在应用程序洞察力中记录为自定义属性,用于在创建范围的生命周期内调用的所有日志。

使用该ILogger对象,您的代码将被简化如下,范围内的所有跟踪和异常都将FileName在应用程序洞察中作为自定义属性。

[FunctionName(nameof(Function1))]
public async Task Run([BlobTrigger("files/{directory}/{name}.00.pdf", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger logger)
{
    using (logger.BeginScope(new Dictionary<string, object>()
    {
            ["FileName"] = name,
     }))
     {
        try
        {
             //some code not related to the issue
        }
        catch (Exception e)
        {
            logger.LogError(e, "Error occurred with {StatusCode}", (long) re.StatusAttributes["x-ms-status-code"]);
            throw;
        }
    }
}

下面总结了语句的定义logger.LogException(e, "Error occurred with {StatusCode}", (long) re.StatusAttributes["x-ms-status-code"]);

  1. e表示发生的实际异常。
  2. 代码部分{StatusCode}将在应用程序洞察中为记录的异常记录 StatusCode 作为自定义属性,因此您无需创建任何字典。
  3. FileName将被记录为范围定义的自定义属性。

您可以在此处查看示例实现。


推荐阅读