【发布时间】:2018-08-15 18:58:48
【问题描述】:
我遇到了一个我不熟悉的问题。
所以我尝试从测试 Azure 函数中记录异常,但是当我抛出异常并返回 400 错误请求时,应用程序洞察力将日志注册为成功请求。
据我了解,它可能是注册函数的成功运行,但我不明白我应该如何记录异常。
所以我到目前为止所做的就是这个。
(从现在开始,我将把 Application Insights 称为 AI)
我首先创建了一个 AI 资源。
然后我拿起仪器键并将其应用于我的功能的应用程序设置。
之后,我将 AI NUGET 安装到我的函数中,创建一个 Projet.json 文件,然后粘贴类似这样的东西,它安装了必要的程序集等。
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.ApplicationInsights": "2.4.0"
}
}
}
}
在此之后,我在函数中初始化 TelemetryClient 并尝试在 catch 中记录和异常:
启动:
string key = TelemetryConfiguration.Active.InstrumentationKey = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);
TelemetryClient telemetry = new TelemetryClient() {
InstrumentationKey = key
};
赶上:
catch (Exception e)
{
Dictionary<string,string> properties = new Dictionary<string,string>();
properties.Add("Function Payload", data.ToString());
properties.Add("Function Exception", e.ToString());
telemetry.TrackException(e, properties);
return req.CreateResponse(HttpStatusCode.BadRequest, e);
}
测试运行我得到的功能:
2018-03-07T14:24:36.171 [Info] Function started (Id=0292b455-314d-4c4c-872a-2b8137a72305)
2018-03-07T14:24:37.092 [Info] Function completed (Success, Id=0292b455-314d-4c4c-872a-2b8137a72305, Duration=931ms)
在应用程序洞察力中,我只能看到对 StatusCode 的错误请求:500 但是 400 个错误请求被记录为成功请求。
而且 TrackException 功能也不会记录任何自定义属性...
那我错过了什么?
有关异常记录的更多详细信息:
【问题讨论】:
标签: azure logging exception-handling azure-functions azure-application-insights