These days for every web application, there has always been the need to monitor application performance, but not always do it well. Similar is the case with the .NET application, but then we found Elastic APM, which is the best solution over this need. Here in this article, we will look at how to use Elastic APM in any .NET application.

What is Elastic APM?

Elastic APM is an application performance monitoring system.
It's a real-time monitoring tool for applications. With Elastic APM, we can determine information on requests, responses, database queries, and external HTTP requests.
Current version available for .NET apps.

Note: For this to work, please ensure that your applications should be targeting .NET framework 4.8.

(https://www.nuget.org/packages/Elastic.Apm)

Using Elastic APM in .NET app.

  1. To use Elastic APM in the .NET application, you need to add ELASTIC.APM package to your application from NuGet and use Public API.

  2. The Elastic APM .NET agent's public API lets you customize and create spans and transactions. It also helps in tracking errors.

  3. Public API allows using three more interrelated API as Tracer, Span, and Transaction.

    • Tracer API gives you access to the currently active transaction and enables you to start it.
    • Span API contains information about a specific code path, executed as part of a transaction.
    • Transaction describes an event captured by an Elastic APM agent monitoring a service. Transactions help combine many Spans into logical groups.
  4. Follow the steps explained in youtube video to integrate Elastic APM in .net core & .NET apps.

    • We need first to update NEWTONSOFT.JSON.

    • Update .CSPROJ file with the NEWTONSOFT.JSON reference. The reference should look like:

    <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>bin\Newtonsoft.Json.dll</HintPath>
    </Reference>
    
    • Add or Update WEB.CONFIG with the following assembly redirect entry:
    <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
    </dependentAssembly>
    
    • Add package Elastic.Apm.AspNetFullFramework via NUGET. After that update two references of Elastic.Apm and Elastic.Apm.AspNetFullFramework in the .CSPROJ :
    <Reference Include="Elastic.Apm, Version=1.3.1.0, Culture=neutral, PublicKeyToken=ae7400d2c189cf22, processorArchitecture=MSIL">
      <HintPath>bin\Elastic.Apm.dll</HintPath>
    </Reference>
    <Reference Include="Elastic.Apm.AspNetFullFramework, Version=1.3.1.0, Culture=neutral, PublicKeyToken=ae7400d2c189cf22, processorArchitecture=MSIL">
      <HintPath>bin\Elastic.Apm.AspNetFullFramework.dll</HintPath>
    </Reference>
    
    • Enable auto instrumentation by including the ElasticApmModule IIS Module in your application’s WEB.CONFIG:
    <?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>
    
    • Now we need to push some packages. Run the following git commands or can follow the approach via visual studio:
    git add packages\Microsoft.Diagnostics.Tracing.TraceEvent.2.0.2\ -f
    git add packages\System.Diagnostics.DiagnosticSource.4.0.0\ -f
    git add packages\System.Threading.Tasks.Dataflow.4.9.0\ -f
    
    • Push your changes and deploy them.

Note:- Reference paths can vary according to respective projects or code.

The overall benefits of using Elastic APM are monitoring of applications, performance tracking, and logging.
For example :- Elastic APM can be used for scenerio when you need to call third party API and check the time of response for getting data, so that API throughput can be monitored.
It's a one-stop solution for multiple things and has good support.

If you face any issue, drop a comment 📝in the comments section below 👇 mentioning the issue and error-details if any.

Thank you. ❤️

References:

Elastic search official site