skip to Main Content

I’m encountering an issue where Azure Function App isn’t forwarding logs to AppInsight. I’ve checked the file system logs on the function app, and they’re working fine and complete. Also, I’m sure that my APPINSIGHTS_INSTRUMENTATIONKEY is configured correctly. Moreover, this only happens in the dev environment; in higher environments, it works fine. I’m using Terraform to create resources. Do you know what possibilities could lead to this issue? (network, role, configuration, etc.) Thanks a lot.

screenshot filesystem log

enter image description here

My host.json

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Information",
      "Host": "Error",
      "Function": "Information",
      "Host.Aggregator": "Information"
    }
  }
}

Startup.cs

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services
        .AddLogging(lb =>
        {
            lb.AddApplicationInsightsWebJobs(options =>
            {
                options.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
            });
        })
    // Already try AddLogging()

}

my csproj

 <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>  
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventGrid" Version="2.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Kafka" Version="3.8.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.2.1" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="2.1.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..EventBroker.ApplicationEventBroker.Application.csproj" />
    <ProjectReference Include="..EventBroker.InfrastructureEventBroker.Infrastructure.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

2

Answers


  1. You don’t need to configure DI for Application Inisghts.

    Application Insights is added by Azure Functions automatically.

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#logging-services

    You just need to configure Application Insights by using either of:

    Login or Signup to reply.
  2. In addition to Vlad DX’s answer check the below steps.

    • Configure the Application Insights from Connected Services.

    enter image description here

    • Instrumentation key is stored in Secrets.json file locally.

    enter image description here

    As mentioned by Vlad DX, you don`t need to add Dependency Injection for Application Insights.

    • I did not add any Startup file, I have tried to run the default Function App and log messages locally.

    • Default host.json worked for me.

    My host.json file:

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

    My .csproj file:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <ApplicationInsightsResourceId>/subscriptions/****/resourceGroups/****/providers/microsoft.insights/components/FunctionApp1</ApplicationInsightsResourceId>
        <UserSecretsId>****</UserSecretsId>
      </PropertyGroup>
      <ItemGroup>
      
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />  
      
        <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>
    
    
    • You can see the traces requested from localhost.

    enter image description here

    Deployed Function Output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search