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

大憨熊 提交于 2019-12-11 08:45:36

问题


Function app is as below:

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

local.json file has applicationInstrument key.

How to add additional field and set "Session_Id" for "Request" entry in application insights.


回答1:


You need to this using some custom logging from Application Insights

First, install the Nuget package

Install-Package Microsoft.ApplicationInsights -Version 2.7.2

Then change your above code like below

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");

        }


    }

Finally in the appinsights

Update 1

You can also add your own additional properties within the request.

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

This will add the properties under the customDimension properties

You can also refer here



来源:https://stackoverflow.com/questions/52027917/how-to-set-session-id-or-create-custom-field-into-applicationinsights-from-funct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!