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
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
You don’t need to configure DI for Application Inisghts.
You just need to configure Application Insights by using either of:
APPLICATIONINSIGHTS_CONNECTION_STRING
https://learn.microsoft.com/en-us/azure/azure-functions/functions-app-settings#applicationinsights_connection_string
APPINSIGHTS_INSTRUMENTATIONKEY
https://learn.microsoft.com/en-us/azure/azure-functions/functions-app-settings#appinsights_instrumentationkey
In addition to Vlad DX’s answer check the below steps.
Secrets.json
file locally.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:My
.csproj
file:Deployed Function Output: