Use hosting startup assemblies in ASP.NET Core allows ASP.NET Core 3.x application to enhance the startup experience. Application Insights Profiler makes use hosting startup to enable zero code activation. Here's how.
- Basic knowledge of how to Dockerize an ASP.NET Core application.
- A valid application insights instrumentation key.
To use hosting startup for your project, there are 3 key elements:
- Reference the NuGet package of
Microsoft.ApplicationInsights.Profiler.AspNetCore
and the latest version of Application Insights SDK. - Set the environment variable of
ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=Microsoft.ApplicationInsights.Profiler.HostingStartup30
. - Set the instrumentation key since it relies on Application Insights.
Neither step requires code change. Adding reference to NuGet package can be done by .NET Core CLI, and setting environment variables by docker instruction. The goal, apparently, is reachable.
Let's begin with the Dockerfile from Dockerize an ASP.NET Core application from docker.com, notice that I've replaced the tag for both base images to 3.1
:
If you choose to skip the process, refer to the final dockerfile and start building the image.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerApp30HS.dll"]
There are small tweaks needed to make it work.
- Firstly, add reference to the NuGet packages, after the project file is copied and before restore happens:
# Add reference to the latest stable version of Application Insights for ASP.NET Core.
RUN dotnet add package Microsoft.ApplicationInsights.AspNetCore --no-restore
# Adding reference to Microsoft.ApplicationInsights.Profiler
RUN dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore -v 2.1.0-* --no-restore
As you already know, invoking dotnet add
does nothing but update the project file. And running COPY . ./
next will overwrite the change. To deal with it, copy everything before adding packages and do not copy it again later.
That should give us a good build image. Refer to Dockerfile for details.
- Then, we will need to setup 2 environment variables in the runtime image:
# Setup environment variable for application insights instrumentation key
ENV APPINSIGHTS_INSTRUMENTATIONKEY=your-instrumentation-key
# Setup environment variable for hosting startup assembly
ENV ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=Microsoft.ApplicationInsights.Profiler.HostingStartup30
After all tweaks, the image is ready to be built. Again, refer to Dockerfile for a complete example.
-
Pull the base images:
docker pull mcr.microsoft.com/dotnet/core/sdk:3.1 docker pull mcr.microsoft.com/dotnet/core/aspnet:3.1
-
Use the following commands to build and run your Docker image:
docker build -t profiler-dotnet30-docker-hs . docker run -d -p 8080:80 --name profiler-dotnet30-docker-hs profiler-dotnet30-docker-hs
-
Generate some traffic to the following url for profiling:
http://localhost:8080/weatherforecast
-
Use the following command to check the logs:
docker logs profiler-dotnet30-docker-hs
The log will look like this:
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
...
info: ServiceProfiler.EventPipe.Client.ServiceProfilerProvider[0]
Service Profiler session started.
...Looking for the line of
Service Profiler session started
to make sure the Profiler is activated. -
To kill the container once you finished verifying it locally:
docker container rm profiler-dotnet30-docker-hs -f
Deploy the images built to Azure App Service and it will run already. What special about running it in Azure is that it enables us to overwrite the environment variables. This is very useful in sense of:
-
Overwrite for the environment variables set in the docker file
-
You could set APPINSIGHTS_INSTRUMENTATIONKEY in app settings and the application will honor it;
- I would setup 2 application insights resource. One for development only, one for production. The production instrumentation key will only be protected and set only in the Azure App service settings.
-
You could set ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to decide whether to light up Application Insights Profiler or not.
-
-
Profiler supports custom configurations through app settings. For example:
- Set
ServiceProfiler__Duration
to00:00:30
will reduce the profiling time to 30 seconds. - Refer to Configurations for more options.
- Set
If there's a problem, logs will be useful. Turn on the logs in the Azure portal: