Before you can monitor your Xamarin application, you will need to instrument your application to enable the Xamarin Agent to collect mobile metrics.
After you have set up and accessed Mobile RUM, follow these instructions:
All other platforms will build and run without errors, but no monitoring will occur. |
Xamarin <= 50.2 only supported the instrumentation of applications that reference the Portable Class Libraries (PCL). From Xamarin >= 50.3, the Xamarin Agent only supports the .NET Standard library and the instrumentation of applications that reference the .NET Standard library. If you still need PCL support, you are recommended to use Xamarin <= 50.2. |
Follow the steps below to manually instrument your Xamarin iOS, Android, and Forms apps.
Xamarin.Android
project, add the following to MainActivity.cs
under OnCreate
:AppDynamics.Droid.Agent.Init(this, bundle); |
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()); } } |
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.
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
.
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); |
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. |
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.
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
int the method Main.
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); ... } } |
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.
For iOS projects with Xamarin Agent >= 21.6.0, you must add the additional If the iOS project file is edited directly, the build configuration should contain the
|
The Xamarin Agent can automatically inject Xamarin-specific instrumentation code into your project. You can:
Xamarin.Forms
pages.Xamarin.Forms
UI elements.This can be achieved using a different beta nuget package called AppDynamics.Agent.AutoInstrument.Fody
.
AppDynamics.Agent.AutoInstrument.Fody
PackageAdd the AppDynamics.Agent.AutoInstrument.Fody
package.
To install the |
After you build the solution, two files should be generated automatically:
FodyWeavers.xml
FodyWeavers.xsd
These files should be checked into source control.
If the above files are not generated automatically, a new file called
If the project is already using For example:
|
The Xamarin SDK has additional classes to allow you to extend the kinds of application data you can collect and aggregate using Mobile RUM. See Customize the Xamarin Instrumentation.
To use an on-premises EUM Server, you pass the URL to the on-premises EUM Server when you initialize the instrumentation with the EUM App Key from Get Your Application Key:
var config = AppDynamics.Agent.AgentConfiguration.Create(<EUM_APP_KEY>); config.CollectorURL = <COLLECTOR_URL:PORT>; AppDynamics.Agent.Instrumentation.InitWithConfiguration(config); |
As new features are added to the agent, you will need to upgrade the Xamarin Agent package in your app.