功能应用如下:
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, ILogger log)
{
log.LogInformation("Information", infoOBject);
}
local.json 文件具有 applicationInstrument key 。
如何添加附加字段并为应用程序洞察中的“请求”条目设置“Session_Id”。
最佳答案
您需要使用 Application Insights 中的一些自定义日志记录来实现此目的
首先,安装Nuget包
Install-Package Microsoft.ApplicationInsights -Version 2.7.2
然后将上面的代码更改如下
public static class Function1
{
private static TelemetryClient GetTelemetryClient()
{
var telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "<your actual insight instrumentkey>";
telemetryClient.Context.Session.Id = "124556";
//update 1-Custom properties- Start
telemetry.Context.Properties["tags"] = "PROD";
telemetry.Context.Properties["userCorelateId"]="1234";
//update 1-Custom properties- Ends
return telemetryClient;
}
[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
{
var appInsights = GetTelemetryClient();
appInsights.TrackRequest(req.RequestUri.ToString(), DateTime.Now, Stopwatch.StartNew().Elapsed, "200", true);
return req.CreateResponse(HttpStatusCode.OK, "message");
}
}
终于进入appinsights
更新 1
您还可以在请求中添加您自己的附加属性。
E.g,
telemetry.Context.Properties["tags"] = "PROD";
这将在 customDimension
属性下添加属性
您还可以refer here
关于c# - 如何从 Function App 设置 session ID 或创建自定义字段到 Application Insights,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52027917/