0

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.Application\EventBroker.Application.csproj" />
    <ProjectReference Include="..\EventBroker.Infrastructure\EventBroker.Infrastructure.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>
8
  • Please share your .csproj file. Also please share your function code. Commented May 13, 2024 at 10:03
  • You are having issue in getting logs when the app is running locally? Commented May 13, 2024 at 10:03
  • You are setting Instrumentation key from Environment variable.Does your local app has environment variable set? Commented May 13, 2024 at 11:15
  • What Azure Function kind are you using (In-process or Isolated)? What .NET version? Commented May 13, 2024 at 21:42
  • Hi @Harshitha, I have already shared .csproj file. I just implement log.LogInformation in function code. I have issue the log that is not forwarded to appinsight. But I already check the filesystem log in function. It work properly Commented May 15, 2024 at 1:31

2 Answers 2

0

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Already tried this. But the log is still not forwarded to App Insight
0

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

3 Comments

@tatea99 - Are you able to log traces ?
Can you check out this question as well? stackoverflow.com/questions/79016060/…
Sure..will check and let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.