ApplicationInsights-dotnet
ApplicationInsights-dotnet copied to clipboard
Application Insights not working when Dockerising a simple WebApp, but works when running the web via HTTPS
Issue Transfer
This issue has been transferred from the Azure SDK for .NET repository, #54369.
Please be aware that @Tushar-karkera is the author of the original issue and include them for any questions or replies.
Details
Application Insights not working when dockerising a simple WebApp, but works when running the web via HTTPS, I tried out lot of things like port mapping and disabling https, none of them seems to be working. It would be helpful if any of you have a clue on what needs to be configured.
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using OpenTelemetry.Metrics;
using Telemetry;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddControllers();
//builder.Services.AddHostedService<BackgroundWorker>();
builder.Services.AddSingleton<ICustomService, CustomService>();
builder.Logging.ClearProviders().AddConsole();
// builder.Services.AddMemoryCache();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddLogging(configure =>
{
//configure.ClearProviders();
configure.AddJsonConsole(opts =>
{
opts.TimestampFormat = "s";
});
});
var appInsightsConnectionString = "xyz";
builder.Logging.AddApplicationInsights(x =>
{
x.ConnectionString = appInsightsConnectionString;
}, y => { });
builder.Services.AddSingleton<ITelemetryInitializer, CustomTelemetry>();
builder.Services.AddApplicationInsightsTelemetry(options =>
{
options.ConnectionString = appInsightsConnectionString;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.MapControllers();
var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
app.Run();
The dockerFile
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
# Install CA certificates as root
USER root
RUN apt-get update && apt-get install -y ca-certificates && update-ca-certificates && rm -rf /var/lib/apt/lists/*
# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["TestWebApplication.csproj", "."]
RUN dotnet restore "./TestWebApplication.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./TestWebApplication.csproj" -c $BUILD_CONFIGURATION -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./TestWebApplication.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestWebApplication.dll"]
launch settings
{
"profiles": {
"http": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5102"
},
"https": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7077;http://localhost:5102"
},
"Container (Dockerfile)": {
"commandName": "Docker",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "https://json.schemastore.org/launchsettings.json"
}
Environment
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>7ed28208-e483-41ff-bfb0-d97e5e5c2537</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>.</DockerfileContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.11" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1" />
<PackageReference Include="OpenTelemetry" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0" />
<PackageReference Include="SharpAbp.Abp.OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="4.6.1" />
</ItemGroup>
</Project>