Set up ASP.NET instrumentation
| Framework | Supported versions |
|---|---|
| ASP.NET (.NET Framework) | 4.6.2–4.8.1 (IIS 10) |
For the full compatibility matrix including supported installation methods, refer to Web frameworks.
To enable tracing for ASP.NET (.NET Framework), install the Elastic.Apm.AspNetFullFramework package, add a reference to the package in your web.config file, and then compile and deploy your application.
Ensure you have access to the application source code and install the
Elastic.Apm.AspNetFullFrameworkpackage.Reference the
Elastic.Apm.AspNetFullFrameworkin your application’sweb.configfile by adding theElasticApmModuleIIS module:<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <modules> <add name="ElasticApmModule" type="Elastic.Apm.AspNetFullFramework.ElasticApmModule, Elastic.Apm.AspNetFullFramework" /> </modules> </system.webServer> </configuration>NoteThere are two available configuration sources. To learn more, see Configuration on ASP.NET.
By default, the agent creates transactions for all HTTP requests, including static content: .html pages, images, and so on.
To create transactions only for HTTP requests with dynamic content, such as
.aspxpages, add themanagedHandlerpreCondition to yourweb.configfile:<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <modules> <add name="ElasticApmModule" type="Elastic.Apm.AspNetFullFramework.ElasticApmModule, Elastic.Apm.AspNetFullFramework" preCondition="managedHandler" /> </modules> </system.webServer> </configuration>NoteTo learn more about adding modules, see the Microsoft docs.
Our IIS module requires:
- IIS 7 or later
- Application pool’s pipeline mode has to be set to integrated (default for IIS 7 and up)
- The deployed .NET application must NOT run under quirks mode. This makes
LegacyAspNetSynchronizationContextthe async context handler and can breakHttpContext.Itemscorrectly restoring when async code introduces a thread switch.
Recompile your application and deploy it.
The
ElasticApmModuleinstantiates the APM .NET Agent on the first initialization. However, there might be some scenarios where you want to control the agent instantiation, such as configuring filters in the application start.To do so, the
ElasticApmModuleexposes aCreateAgentComponents()method that returns agent components configured to work with ASP.NET Full Framework, which can then instantiate the agent.For example, you can add transaction filters to the agent in the application start:
using Elastic.Apm; using Elastic.Apm.Api; using Elastic.Apm.AspNetFullFramework; public class MvcApplication : HttpApplication { protected void Application_Start() { // other application startup for example, RouteConfig, and so on. // set up agent with components var agentComponents = ElasticApmModule.CreateAgentComponents(); Agent.Setup(agentComponents); // add transaction filter Agent.AddFilter((ITransaction t) => { t.SetLabel("foo", "bar"); return t; }); } }Now, the
ElasticApmModulewill use the instantiated instance of the APM .NET Agent upon initialization.
After adding the agent, configure it to connect to your APM Server. The fastest way is through environment variables or web.config. See Minimum configuration for the three settings every deployment needs.
For the full list of configuration options available to ASP.NET applications, see Configuration on ASP.NET.