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.
Using Elastic APM in .NET app.
To use Elastic APM in the .NET application, you need to add ELASTIC.APM package to your application from NuGet and use Public API.
The Elastic APM .NET agent's public API lets you customize and create spans and transactions. It also helps in tracking errors.
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.
Follow the steps explained in youtube video to integrate Elastic APM in .net core & .NET apps.
We need first to update
.CSPROJfile with the
NEWTONSOFT.JSONreference. The reference should look like:
<Reference Include="Newtonsoft.Json, Version=184.108.40.206, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <HintPath>bin\Newtonsoft.Json.dll</HintPath> </Reference>
- Add or Update
WEB.CONFIGwith the following assembly redirect entry:
<dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-220.127.116.11" newVersion="18.104.22.168" /> </dependentAssembly>
- Add package
Elastic.Apm.AspNetFullFrameworkvia NUGET. After that update two references of
<Reference Include="Elastic.Apm, Version=22.214.171.124, Culture=neutral, PublicKeyToken=ae7400d2c189cf22, processorArchitecture=MSIL"> <HintPath>bin\Elastic.Apm.dll</HintPath> </Reference> <Reference Include="Elastic.Apm.AspNetFullFramework, Version=126.96.36.199, Culture=neutral, PublicKeyToken=ae7400d2c189cf22, processorArchitecture=MSIL"> <HintPath>bin\Elastic.Apm.AspNetFullFramework.dll</HintPath> </Reference>
- Enable auto instrumentation by including the
ElasticApmModule IISModule 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. ❤️