Help APM APM for .NET Use .NET Agent API for Custom Instrumentation

Use .NET Agent API for Custom Instrumentation

.NET Agent API for Custom Instrumentation

APM Insight .NET Agent API helps to track the user defined methods in a web application. It helps instrument specified methods in the web application DLLs for monitoring its performance. It can also be used to track specific parts of code.    

Steps to Add the API

  1. Add a reference to the library DotNetAgent.Api.dll to your web application project.
  2. The dll is available as a NuGet package.
  3. The API contains a class named CustomTracker to track the performance of a method.
  4. Also the ApiParameter class containing the Method name, Class name and Component name  to be passed as the parameter in CustomTracker.

    CustomTracker Class and its Methods

    Constructors

    CustomTracker(Type thisType)

    • thisType - The type of current class or base class.

              eg: CustomTracker dotNetAgentCustomTracker = new CustomTracker(this.GetType());

    CustomTracker(Type thisType, ApiParameters apiParameters)

    • thisType - The type of current class or base class.
    • apiParameters - Parameter values like class name, method name and component name for the monitoring method.

              eg: var customTracker = new CustomTracker(this.GetType(), new ApiParameters() { MethodName = "GetEmployeeReport", ComponentName = "REPORT" });

    Methods

    CustomTracker.StartTracker(Type thisType)

    • thisType - The type of current class or base class.
    • It is not required since it is called in constructor itself.

    CustomTracker.StartTracker (Type thisType, ApiParameters apiParameters)

    • This method is used to start the metric collection for the custom method.
    • It is not required as it is called in constructor itself.

    CustomTracker.StopTracker()

    • The metric collection will be stopped on calling this method.
    • Always call it in a finally block.

    CustomTracker.TraceError(System.Exception ex)

    • Tracking exception in web transactions.
    • It is a static method to be called on catch blocks.
  5. Create an instance of CustomTracker class at the beginning of a method and invoke StopTracker() at the end of the method.
  6. We can create CustomTracker instance with using{} block. The StopTracker() method will be called when disposing object automatically.

The following examples show the usage of this CustomTracker:

Example 1: Using the "using" statement:

protected void Page_Load(object sender, EventArgs e)
{
using (var customTracker = new CustomTracker(this.GetType(), new ApiParameters() { MethodName = "GetBasicDetails", ComponentName= "DETAILS" }))
     {
AdminBL adminBL = new AdminBL();
if(!IsPostBack)
     {
      ASPSite.BL.MYSQLReference.BasicDetails basicDetails = adminBL.getBasicDetails(id);
      EmpApp.Models.BasicDetails basicDetailsModel = getBasicDetailsModel(basicDetails);
     }
}
}

Example 2: Using StartTracker and StopTracker within a try finally block:

public ActionResult BasicDetails(int id = 0)
{
    CustomTracker customTracker = null;
    AdminBL adminBL = new AdminBL();
    try
     {
        customTracker = new CustomTracker(base.GetType(),new ApiParameters(){ MethodName = "BasicDetails", ComponentName= "DETAILS" });
        ASPSite.BL.MYSQLReference.BasicDetails basicDetails = adminBL.getBasicDetails(id);
        EmpApp.Models.BasicDetails basicDetailsModel = getBasicDetailsModel(basicDetails);
     }
    finally
    {
    customTracker.StopTracker();
    }
return View(basicDetailsModel);
}

Example 3: Using CustomTracker to Instrument part of a code.

public ActionResult BasicDetails(int id = 0)
{
            AdminBL adminBL = new AdminBL();
            using(CustomTracker customTracker = new CustomTracker(base.GetType(),new ApiParameters() { MethodName = "BasicDetails", ComponentName = "DETAILS" }))
             {
//Instrumenting part of a code. To check the time taken by the function FetchAllEmployees and the SQL calls made from this function.
using(CustomTracker fetchAllEmpTracker = new CustomTracker(base.GetType(),new ApiParameters() { MethodName = "FetchAllEmployees", ComponentName = "DETAILS_DB" }))
            {
     var empList = FetchAllEmployees();
            }
ASPSite.BL.MYSQLReference.BasicDetails basicDetails = objadmin.getBasicDetails(id);
EmpApp.Models.BasicDetails basic = getBasicDetailsModel(basicDetails);
}
return View(basicDetailsModel);

Example 4: Using TraceError to trace exceptions.

public ActionResult BasicDetails(int id = 0)
{
            try
            {
            AdminBL adminBL = new AdminBL();
     var empList = FetchAllEmployees();

ASPSite.BL.MYSQLReference.BasicDetails basicDetails = objadmin.getBasicDetails(id);

EmpApp.Models.BasicDetails basic = getBasicDetailsModel(basicDetails);
}
            catch (Exception ex)
            {
            //Exception will be traced for this transaction
            DotNetAgent.Api.CustomTracker.TraceError(ex);
            }

  1. If the method name or class name is not given in the CustomTracker, it will attempt to get the current method name and class name by itself.
  2. The StartTracker() method will be called in constructor by default.
  3. If the agent not installed or the agent service stopped the invoked methods will have no effect.
Was this document helpful?
Thanks for taking the time to share your feedback. We’ll use your feedback to improve our online help resources.

Help APM APM for .NET Use .NET Agent API for Custom Instrumentation