C# 如何从FunctionApp设置会话id或在ApplicationInsights中创建自定义字段

C# 如何从FunctionApp设置会话id或在ApplicationInsights中创建自定义字段,c#,azure,azure-functions,azure-application-insights,telemetry,C#,Azure,Azure Functions,Azure Application Insights,Telemetry,功能应用程序如下所示: public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, ILogger log) { log.LogInformation("Information", infoOBject); } 公共静态异步任务运行([H

功能应用程序如下所示:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, ILogger log)
    {
     log.LogInformation("Information", infoOBject);
    }
公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,“get”,Route=null)]HttpRequestMessage请求,ILogger日志)
{
log.LogInformation(“信息”,infoOBject);
}
local.json文件具有applicationInstrument密钥


如何在application insights中添加附加字段并为“请求”条目设置“会话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");

        }


    }
这将在
customDimension
属性下添加属性


您也可以

记录两次请求。默认情况下已创建一个请求。可能是因为配置了instrumentKey。通过执行上述操作,它会创建另一个请求。只有一个“请求”和sessionId的解决方案?另外,我们可以在“请求”中添加与session_id类似的附加列。例如,userCorelateId列?@user3711357这里的两个请求是什么意思?我只能看到一个请求,它是由
appInsights.TrackRequest
@user3711357创建的。我已经通过添加自定义属性更新了我的答案。希望此帮助说明需要调用appInsights.Flush()还是不需要?
E.g,
telemetry.Context.Properties["tags"] = "PROD";