c# - 我的 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 下面的屏幕截图中看到的那样:
有没有办法可以关闭异常的自动记录?或者有没有办法自定义这些我不知道的异常消息,而不是使用遥测服务?
解决方案
我相信,由于以下原因,正在记录 3 个异常:
IGremlinService
正在记录的抛出异常的实现服务。- 您正在通过登录
_telemetryClient.TrackException(e, properties);
- Azure 基础结构正在处理何时
throw
被调用。
现在来回答你的问题
我发现无法自定义消息或异常发送的信息
我建议您使用ILogger
LogException 并使用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"]);
e
表示发生的实际异常。- 代码部分
{StatusCode}
将在应用程序洞察中为记录的异常记录 StatusCode 作为自定义属性,因此您无需创建任何字典。 FileName
将被记录为范围定义的自定义属性。
您可以在此处查看示例实现。
推荐阅读
- javascript - 无法在纸张列表框上触发铁选择事件
- c# - 将文件上传到 WebApi 并保存,在 IE 中有效,但在 Chrome 中无效
- git - git commit 不适用于 Git Bash
- javascript - 如何从 Firebase 获取 PWA 数据
- angular - 如何在运行时更改 Angular Material Datepicker 格式
- python - python tkinter Radiobuttons - 在显示之前选择两个按钮
- c# - 带有可选参数的 C# 编写的 VBA 函数(COM 插件)
- jquery - CSS 设计无法创建样式和徽标
- peoplesoft - 将用户从 PeopleSoft HRMS 同步到外部应用程序
- javascript - 如何在 React 中使用路由?