如何在 Function App 的 Application Insights 中设置会话 ID 或创建自定义字段

How to set session id or create custom field into ApplicationInsights from FunctionApp

功能应用如下:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, ILogger log)
    {
     log.LogInformation("Information", infoOBject);
    }

local.json 文件有 applicationInstrument 密钥。

如何在 application insights 中为 "Request" 条目添加附加字段并设置 "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