帮助手册 APM APM -.NET 使用 .NET 代理 API 进行自定义检测

使用 .NET 代理 API 进行自定义检测

用于自定义检测的 .NET 代理 API

APM Insight .NET 代理 API 有助于跟踪 Web 应用程序中用户定义的方法。它有助于检测 Web 应用程序 DLL 中的指定方法以监控其性能。它还可用于跟踪代码的特定部分。    

添加 API 的步骤

  1. 将对库 DotNetAgent.Api.dll的引用添加 到您的 Web 应用程序项目中。
  2. 该 dll 以NuGet 包的形式提供。
  3. API 包含一个名为CustomTracker的类,用于跟踪方法的性能。
  4. 还有ApiParameter类,其中包含要作为 CustomTracker 中的参数传递的方法名称、类名称和组件名称。

    CustomTracker 类及其方法

    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. 在方法的开头创建 CustomTracker 类的实例,并在方法的结尾调用 StopTracker()。
  6. 我们可以使用 using{} 块创建 CustomTracker 实例。自动处理对象时会调用 StopTracker() 方法。

以下示例显示了此 CustomTracker 的用法:

示例 1:使用“using”语句:

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);
     }
}
}

示例 2:在 try finally 块中使用 StartTracker 和 StopTracker:

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);
}

示例 3:使用 CustomTracker 检测代码的一部分。

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. 如果 CustomTracker 中没有给出方法名或类名,它将尝试自己获取当前的方法名和类名。
  2. 默认情况下将在构造函数中调用 StartTracker() 方法。
  3. 如果未安装代理或代理服务停止,则调用的方法将无效。

帮助手册 APM APM -.NET 使用 .NET 代理 API 进行自定义检测