On this page:

Your Rating:
Results:
PatheticBadOKGoodOutstanding!
14 rates

The following sections show you how to use the Xamarin SDK to customize your instrumentation.

Because the agent stores data about events in a local buffer before reporting the information, you are recommended to use the APIs with discretion.

For the complete SDK API documentation, see the latest Xamarin SDK documentation or the previous versions listed below:

Track Calls

You can instrument methods to see how often the instrumented a method is invoked and how long it takes to run. To do this, add a call at the beginning and end of the method you'd like to instrument.

In the example below, the code executed in the constructor for the class MyClass will be tracked and reported. In your own code, start tracking calls by specifying the class and method in BeginCall and then complete the tracking and report the data by calling ReportCallEnded.

public class MyClass {
    public MyClass() {
        var tracker = AppDynamics.Agent.Instrumentation.BeginCall("MyClass", "Constructor");
        // The code placed here will be tracked and reported.
        tracker.ReportCallEnded();
    }
}

Timing Events

Sometimes you want to time an event in your application that spans multiple methods. You can do this by calling StartTimerWithName when the event starts, and then StopTimerWithName when it ends. For example, to track the time a user spends viewing a screen, the instrumentation might look something like the following:

async private void StartCapturePreview_Click(object sender, RoutedEventArgs e) {
    capturePreview.Source = captureManager;
    AppDynamics.Agent.Instrumentation.StartTimerWithName("CapturePreview");
    await captureManager.StartPreviewAsync();
}
async private void StopCapturePreview_Click(object sender, RoutedEventArgs e) {
    await captureManager.StopPreviewAsync();
    AppDynamics.Agent.Instrumentation.StopTimerWithName("CapturePreview");
} 

Report Metrics

To report other types of data, you can use a metric. The metric name should only contain alphanumeric characters and spaces. Illegal characters are replaced by their ASCII hex value. The metric value must be a long integer.

The snippet below shows how you might report a metric.

AppDynamics.Agent.Instrumentation.ReportMetricWithName("Database Rows", 5123);

HTTP Requests

You can report a Network Request using the AppDynamics.Agent.HTTPRequestTracker class.

The following is an example of using AppDynamics.Agent.HTTPRequestTracker with the System.Net.Http.HttpClient class. The tracker object synchronously captures and reports the network request as well as any network errors.

public async Task<string> Fetch(Uri uri) {
  var client = new HttpClient();
  // Create AppDynamics Tracker
  var tracker = AppDynamics.Agent.HTTPRequestTracker.Create(uri);
  // Add AppDynamics Server Correlation Headers
  foreach (var header in AppDynamics.Agent.ServerCorrelationHeaders.Generate) {
      // Each header could have multiple values
      foreach (var value in header.Value) {
          client.DefaultRequestHeaders.Add(header.Key, value);
      }
  }
  HttpResponseMessage response = null;
  try {
      response = await client.GetAsync(uri);
  } catch (Exception ex) {
      // Capture any network errors.
      tracker.Exception = ex;
      tracker.ReportDone();
      throw ex; //you decide to throw it or not
  }
  if (!response.Equals(null)) {
      // Capture request information such as the 
      // status code, status message, and headers.
      tracker.ResponseCode = (int)response.StatusCode;
      tracker.StatusLine = response.ReasonPhrase;
      tracker.ResponseHeaderFields = response.Headers;
      tracker.ReportDone();
      return await response.Content.ReadAsStringAsync();
   }
   return null;
}

Leave Breadcrumbs

You can leave breadcrumbs to mark interesting events. For example, if your application crashes, the breadcrumbs you left with be displayed in the crash report and could provide context. You can also configure the breadcrumb to appear in sessions.

The following is the method signature for leaving breadcrumbs:

static void AppDynamics.Agent.Instrumentation.LeaveBreadcrumb(string breadcrumb, BreadcrumbVisibility mode)

You use the mode to set the visibility of the breadcrumb. The visibility defines where you will see the breadcrumb in the Controller UI. The value of mode can be one of the following:

  • BreadcrumbVisibility.CrashesOnly – The breadcrumb will only appear in crash snapshots.
  • BreadcrumbVisibility.CrashesAndSessions  – The breadcrumb will appear in crash snapshots and sessions.

Thus, you would use the method below to set breadcrumbs that are only reported in crash reports:

static void AppDynamics.Agent.Instrumentation.LeaveBreadcrumb("GetUserInfo", BreadcrumbVisibility.CrashesOnly);

If you would like to see the breadcrumb in crash reports and sessions: 

static void AppDynamics.Agent.Instrumentation.LeaveBreadcrumb("GetUserInfo", BreadcrumbVisibility.CrashesAndSessions);

Add User Data

You can set a key/value pair of strings to record important events or information. Below is the method signature for setting user data:

 

static void AppDynamics.Agent.Instrumentation.SetUserData(string key, string value)

For example, you might want to log the user ID when the method for logging in the user is called:

void LogInUser(UserCredentials) {
   // Log in user
   ...
   // Set user data with the user name.
   Instrumentation.SetUserData("user_id", UserCredentials.ID);
}

This information is available in Network Request Analyze and is added to any crash snapshots that may be taken. Keys and values are limited to 2048 characters each.

You can also set user data with values of other types (long, boolean, double, DateTime) using the following methods:

To remove user data, use the following methods:

  • No labels