How to set session id or create custom field into

2019-07-24 08:13发布

问题:

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