Instrument Xamarin Applications

Follow the steps below to manually instrument your Xamarin iOS, Android, and Forms apps.
Add the Xamarin Agent Package
  1. Get the Xamarin Agent from the NuGet Gallery. Follow the instructions given in Adding a Package to add the package Splunk AppDynamics Xamarin Agent from nuget.org.

  2. Add the Xamarin.Forms package from AppDynamics.Agent.Forms.

  3. In theXamarin.Android project, add the following to MainActivity.cs under OnCreate :

AppDynamics.Droid.Agent.Init(this, bundle);
Example
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);  //existing code
global::Xamarin.Forms.Forms.Init(this, bundle);
AppDynamics.Droid.Agent.Init(this, bundle); //initialize the agent on the Android Platform
LoadApplication(new App());
}
}

Get Your Application Key

Complete the Getting Started Wizard to get an EUM App Key. You will need this key when you modify the source code. In some cases, multiple mobile applications can share the same key.

Because there is no Xamarin platform option, you will need to choose either Android or iOS. For Android, you will need to select Manual.

If you have completed the Getting Started Wizard, but don't have your EUM App Key, see Get Your Application Key.

Add the Required Permissions (Android Deployments Only)

Open the file Properties/AndroidManifest.xml and verify that it has these permissions:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

If these permissions are not present, add them to the file Properties/AndroidManifest.xml .

Initialize the Agent

To initialize the Xamarin Agent, you use the code below for iOS and Android. Use the EUM app key (enter as a string) that you received after completing step 2.

var config = AppDynamics.Agent.AgentConfiguration.Create(<EUM_APP_KEY>);
AppDynamics.Agent.Instrumentation.InitWithConfiguration(config);
Note: If you are running an on-premises EUM Server, you need to specify the URL to the EUM Server. See

Point to an On-Premises EUM Server (Optional)

to learn how.
Forms Solution

For Forms Solutions, you only need to place the initialize code in the constructor of the App.xaml.cs file for the Xamarin Agent to instrument both Android and iOS applications.

public App()
{
InitializeComponent();
// This initialization code is used by both iOS and Android apps.
var config = AppDynamics.Agent.AgentConfiguration.Create(<EUM_APP_KEY>);
AppDynamics.Agent.Instrumentation.InitWithConfiguration(config);
MainPage = new FormsExamplePage();
}

If you have application code in the MainActivity.cs file for Android or AppDelegate.cs for iOS that you want to instrument, however, you should initialize the Xamarin Agent in those files as you would do for iOS Solutions and Android Solutions.

iOS Solution

For iOS apps, you place the initialize code In the AppDelegate.cs file in the method FinishedLaunching of the class AppDelegate as shown below.

public class AppDelegate : UIApplicationDelegate
{
// class-level declarations
public override UIWindow Window
{
get;
set;
}
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// The two lines below initialize the AppDynamics instrumentation.
var config = AppDynamics.Agent.AgentConfiguration.Create(<EUM_APP_KEY>);
AppDynamics.Agent.Instrumentation.InitWithConfiguration(config);
...
return true;
}
...
}

You may also consider placing it in the Main.cs Main.

Android Solution

In the MainActivity.cs file, place the initialization code in the method OnCreate:

class MainActivity {
protected override void OnCreate(Bundle savedInstanceState) {
// The two lines below initialize the AppDynamics instrumentation.
var config = AppDynamics.Agent.AgentConfiguration.Create(<EUM_APP_KEY>);
AppDynamics.Agent.Instrumentation.InitWithConfiguration(config);
...
}
}

Build the Application

Run and build your application from Visual Studio. From the Getting Started Wizard, you should see that the application has connected and the instrumentation has been verified.

Note:

For iOS projects with Xamarin Agent >= 21.6.0, you must add the additional MtouchExtraArgs --registrar:static for each simulator build configuration, such as the debug and release builds. This does not apply for physical device configurations since the static registrar is already the default.

If the iOS project file is edited directly, the build configuration should contain the MtouchExtraArgs element:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' "> ... <MtouchExtraArgs>--registrar:static</MtouchExtraArgs> </PropertyGroup> "