如何为 Azure Function 中的 Application Insight 中的请求设置 user_AuthenticatedId?
我使用 Azure Functions 为移动客户端提供服务,在它们之间有一个 Azure API 管理。我还使用 Application Insights 来监视传入后端的请求。默认日志记录会在 AppInsights 中创建请求,但某些字段(例如 user_AuthenticatedId
)为空,这对我来说可能有用。另外,在 AppInsights 的“用户”页面上,我只能看到一个 id 为“<未定义>”的用户
在 Azure Function 中,我获得一个访问令牌,其中包含用户的 id,我想将其设置为 user_AuthenticatedId
。我尝试配置 TelemetryClient
并启动一个新的 Operation
,其中设置了 operation.Telemetry.Context.User.AuthenticatedUserId = _userID;
但使用此请求在 AppInsights 上重复。
是否可以从Azure函数的代码中设置默认创建的请求遥测的属性?
我的函数方法如下所示:
[FunctionName("TestFunction")]
public async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, HttpMethod.Get)] HttpRequest req, ILogger log)
{
try
{
//do some stuff
return new OkObjectResult(<result>);
}
catch (Exception ex)
{
//handle exception
return new ObjectResult(<result>);
}
}
I'm using Azure Functions to serve mobile clients and between them, there is an Azure API Management. I also use Application Insights to monitor requests coming to the backend. The default logging creates requests in AppInsights but some field (for example user_AuthenticatedId
) is empty which could be useful for me. Also on the Users page in AppInsights I can only see one user where the id is "<undefined>"
Requests in Application Insight look like this
In the Azure Function, I get an access token that contains the id of the user and I want to set this as the user_AuthenticatedId
. I tried to configure a TelemetryClient
and start a new Operation
where I set the operation.Telemetry.Context.User.AuthenticatedUserId = _userID;
but with this, the request was duplicated on AppInsights.
Is it possible from the code of the Azure Function to set the properties of the request telemetry which is created by default?
My Function methods look like this:
[FunctionName("TestFunction")]
public async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, HttpMethod.Get)] HttpRequest req, ILogger log)
{
try
{
//do some stuff
return new OkObjectResult(<result>);
}
catch (Exception ex)
{
//handle exception
return new ObjectResult(<result>);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在日志终端中查看,请使用以下代码片段:
要在应用程序洞察中查看,请使用
TrackTrace
或TrackEvent
:这里
tc
是遥测客户端。例如,这是我的示例代码:
To see in the logs terminal, use below code snippet :
To see it in application insights, use
TrackTrace
orTrackEvent
:Here
tc
is Telemetry Client.For example, here is the sample code I have :