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

时间:2018-08-26 15:55:14

标签: 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);
    }

local.json文件具有applicationInstrument密钥。

如何在应用程序见解中添加其他字段并为“请求”条目设置“ Session_Id”。

1 个答案:

答案 0 :(得分:1)

您需要使用一些来自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

enter image description here

更新1

您还可以在请求中添加自己的其他属性。

E.g,
telemetry.Context.Properties["tags"] = "PROD";

这将在customDimension属性下添加属性

enter image description here

您也可以refer here