2

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.

Jayendran
  • 9,638
  • 8
  • 60
  • 103
user3711357
  • 1,425
  • 7
  • 32
  • 54

1 Answers1

2

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

enter image description here

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

enter image description here

You can also refer here

Jayendran
  • 9,638
  • 8
  • 60
  • 103
  • this log two request. One request is already created by default. May be because of configured, instrumentKey. By doing above stuff, it creates another request. Any solution for, only one "request" and having sessionId ? – user3711357 Aug 27 '18 at 15:48
  • also, can we add additional column to "Request" as similar to session_id. Like, userCorelateId column ? – user3711357 Aug 27 '18 at 15:52
  • @user3711357 what do you mean by two requests here? I could see an only single request, which is created by `appInsights.TrackRequest` – Jayendran Aug 28 '18 at 03:32
  • @user3711357 I've updated my answer by using adding the custom properties. Hope this helps – Jayendran Aug 29 '18 at 10:09
  • Is there need to call appInsights.Flush() or its not require ? – user3711357 Aug 30 '18 at 14:12
  • It based on the situation.I've provided the detailed answer to your [question](https://stackoverflow.com/questions/52099180/is-there-memory-impact-or-performance-cause-without-telemetryclient-flush) – Jayendran Aug 30 '18 at 15:22