Home > Blockchain >  Application Insights not working in locally running Azure Worker Function
Application Insights not working in locally running Azure Worker Function

Time:01-05

I am trying to get app insights to work in a local .NET 7 Worker Azure Function

public class Tools
{
    public Tools(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<Tools>();
    }
    
    [Function("test-app-insights")]
    public async Task<IActionResult> TestAppInsightsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "tools/test-app-insights")]
            HttpRequest req)
    {
        _logger.LogInformation("HELLOFROMNEWFUNCTION");
        await Task.Yield();

        var response = 0;
        var okResponse = new OkObjectResult(response);
        return okResponse;
    }
}

I have added the code below to configure services but nothing is shown in App Insights, and there are no errors

        var appInsightsConnectionString = "MY CONNECTION STRING";

        services.AddApplicationInsightsTelemetryWorkerService((a =>
        {
            a.EnableAdaptiveSampling = false;
            a.ConnectionString = appInsightsConnectionString;
        }));

Does anyone know what I have missed?

I have this in the logging section of my host.json

"logging": {
    "logLevel": {
      "default": "Information",
      "Microsoft": "Warning",
      "System": "Warning",
      "Host": "Error",
      "Function": "Error",
      "Host.Aggregator": "Information"
    },
    "Serilog": {
      "MinimumLevel": "Information",
      "WriteTo": [
        {
          "Name": "Console",
          "Args": {
            "outputTemplate": "{Timestamp:HH:mm:ss} {Level} | {RequestId} - {Message}{NewLine}{Exception}"
          }
        }
      ]
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true
      }
    }
  },

Paul

CodePudding user response:

Create Application Insights Instance and copy the Instrumentation key for later use.

enter image description here

  • Create a Function App. enter image description here

  • Enable Application Insights option while creating the Function App.

  • Select the Application Insights which we have created in the previous step.

enter image description here

  • Make sure that the region of Application Insights and Function App are same.
  • InstrumentationKey will be added in the Application Settings automatically once we enable the ApplicationInsights.

enter image description here

  • In Visual Studio, Create a Function App with .NET 7 Isolated.

My host.json:

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}

My local.settings.json:

{

"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"APPINSIGHTS_INSTRUMENTATIONKEY": "Copy from Application Insights "
 }
}

My Traces in Azure Application Insights Instance:

enter image description here

  • Related