How to implement the Elastic APM tool in .NET applications

Sebastian Restrepo Moreno
Condor Labs Engineering
4 min readJun 2, 2022

In order to implement the APM tool, it’s necessary to know about its general conceptualization and to visualize the benefits we can obtain from it.

APM is responsible for monitoring the performance and availability of software applications. APM attempts to detect and diagnose complex application performance problems to maintain expected service levels.

In its conceptual framework, APM is responsible for:

  • User experience monitoring
  • Application runtime architecture modeling and exploration
  • User-defined transaction logs
  • Application component monitoring
  • Application data analysis and reporting

Some of the metrics that APM can provide us with are: application errors, response times, and database queries, among others, all presented in nice graphs.

(Illustrative image of an Elastic APM panel)

There are two types of APM (Application Performance Management):

  • Traditional APM software is installed on your premises to monitor an application, and infrastructure in your network/data center.
  • Cloud APM does not require you to purchase software; instead, you use the APM provider’s cloud instance to configure and monitor your applications.

In this case, we will use APM in the cloud provided by Elastic to implement the tool in .NET applications.

If your .NET Framework is a version higher than 5.0, you can follow these steps to make it happen, otherwise, we will detail the process to be performed for other versions later.

  1. Download the APM agent:

Add the agent package(s) from NuGet to your .NET application. There are multiple NuGet packages available for different use cases.

For an ASP.NET Core application with Entity Framework Core download the Elastic.Apm.NetCoreAll package. This package will automatically add every agent component to your application.

In case you’d like to minimize the dependencies, you can use the Elastic.Apm.AspNetCore package for just ASP.NET Core monitoring or the Elastic.Apm.EfCore package for just Entity Framework Core monitoring.

In case you only want to use the public Agent API for manual instrumentation use the Elastic.Apm package.

2. Add the agent to the application:

In the case of ASP.NET Core with the Elastic.Apm.NetCoreAll package, call the UseAllElasticApm method in the Configure method within the Startup.cs file.

public class Startup 
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAllElasticApm(Configuration);
//…rest of the method
}
//…rest of the class }

Passing an IConfiguration instance is optional and by doing so, the agent will read config settings through this IConfiguration instance (e.g. from the appsettings.json file).

3. Sample appsettings.json file:

{     
"ElasticApm": {
"SecretToken": "YOUR_TOKEN",
"ServerUrls": "YOUR_URL", //Set custom APM Server URL (default: http://localhost:8200)
"ServiceName": "MyApp", //allowed characters: a-z, A-Z, 0-9, -, _, and space. Default is the entry assembly of the application "Environment": "production", // Set the service environment
}
}

In case you don’t pass an IConfiguration instance to the agent (e.g. in the case of non ASP.NET Core applications) you can also configure the agent through environment variables. See the documentation for advanced usage.

That’s it, at this point you should be able to deploy your application and your metrics will be displayed in your dashboard.

For the second scenario we will see how to implement APM in .NET frameworks of versions lower than 5.0:

The first thing we must perform or validate is if our framework is higher than 4.6.1. If it’s not, we will perform the version update.

In some cases, when generating the version update, some blocks of code are duplicated, which will generate conflicts, if you present some error you will have to validate this.

Install the Elastic.Apm.AspNetFullFramework package

Reference Elastic.Apm.AspNetFullFramework in your application’s web.config file by adding the IIS module ElasticApmModule:

<configuration><system.webServer><modules><add name="ElasticApmModule" type="Elastic.Apm.AspNetFullFramework.ElasticApmModule, Elastic.Apm.AspNetFullFramework" /></modules></system.webServer></configuration>

Finally, if you need to add connection variables, you must do it in the web.config file in the keys section:

<configuration><!-- ... --><appSettings><!-- ... →<!-- Elastic APM tool Settings →<add key="ElasticApm:ServerUrl" value="https://your_url.com" /><add key="ElasticApm:SecretToken" value="your_token" /><add key="ElasticApm:ServiceName" value="the_name_of_your_app" /><add key="ElasticApm:Environment" value="Test" /><!-- ... --></appSettings><!-- ... --></configuration>

After you have done this, all that remains is to deploy your application and you should have metrics in your dashboard.

In conclusion, the Elastic APM tool is an important tool for developers because it makes it possible to understand all the behavior and flow that our applications have. It also allows us to identify points of improvement within our applications thanks to its metrics of latency and times between requests, be it HTTP requests or requests generated to the databases.

In this regard, it should always be remembered that:

“What cannot be measured cannot be improved and what is not improved is always degraded!”
William Thompson Kelvin

Thank you for getting to this point. I hope I have helped you to understand a little more about how beneficial metrics are in your projects. If you want to go deeper into this topic you can do it directly by reading the official documentation of Elastic APM: https://www.elastic.co/guide/index.html 👏🏼

--

--

Sebastian Restrepo Moreno
Condor Labs Engineering

TypeScript Backend Developer | AWS Cloud Practitioner Certified. 🚀 💻