Search code examples
asp.net-mvcazure-devopsazure-web-app-serviceazure-application-insightsazure-log-analytics

Can't send customMetrics and customEvents data to Application insights


Application telemetry is my new area. I can't send my custom data to Application insights to query and monitor. I need to send an user ID metrics to application insights who access my web-application written in .NET Core MVC model. I've added a code after referencing from articles out there and from official documentation, once I add these custom Code under my controller the default data that used to send to insights is stopped

I've tried added the following code under a controller. NOTE: This is for testing whether I can able to send metrics of my own. I too added some static data to test.

I've passed the TelemetryClient by DI method

        try
        {
            // Track a custom metric
            telemetryClient.GetMetric("CustomMetricID").TrackValue(userid);
            telemetryClient.GetMetric("TestMetric").TrackValue(1);
            // Set the user and organization IDs
            telemetryClient.Context.User.Id = userid.ToString();

           // Track a custom event
            var customEvent = new EventTelemetry("CustomEvent_UserID");
            customEvent.Properties.Add("Int_USER_ID", userid.ToString());
            customEvent.Properties.Add("Desc", "Working...");
            telemetryClient.TrackEvent(customEvent);
        }
        catch (Exception ex)
        {
            telemetryClient.TrackException(ex);
        }
        finally
        {
            telemetryClient.Flush();
        }

I'm expecting to send my custom data (here USER_ID) to application insights.


Solution

  • TelemetryChannel found a telemetry item without an InstrumentationKey. This is a required field and must be set in either your config file or at application startup_

    • The code which you have configured is correct.

    • We need to add the Application Insights ConnectionString/InstrumentationKey in the appsettings.json file.

    enter image description here

    appsettings.json

    {
      "Logging": {
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Debug",
            "Microsoft": "Error"
          }
        },
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*",
      "ApplicationInsights": {   
      "ConnectionString": "InstrumentationKey=56**9q4-****-4**9-8a4f-a******d6a;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/"
      }
    }
    

    Install the NuGet package Microsoft.ApplicationInsights.AspNetCore.

    .csproj file:

    <PackageReference  Include="Microsoft.ApplicationInsights.AspNetCore"  Version="2.21.0" />
    
    • Add the below line in Pogram.cs file.
    builder.Services.AddApplicationInsightsTelemetry();
    
    • Make sure TelemetryClient is configured correctly with the InstrumentationKey.

    • In Controller create an Instance of TelemetryClient .

     public readonly TelemetryClient mytelemetry;
    

    OR

    public TelemetryClient mytelemetry = new TelemetryClient();
    
    public MyController(TelemetryClient telemetryClient) 
    {
       mytelemetry= telemetryClient; 
    }