Mobile apps are used by the end user and it's difficult to troubleshoot or analyze an error that occurred on their device. So, when developing mobile apps, it's always a good idea to have diagnostic logs to analyze errors and app crashes. In this article, we will integrate app dynamics with the Xamarin mobile app to do verbose logging and crash reports.
To begin with, create an account with app dynamics. Its provides two options to sign up for app dynamics: SaaS and On-Premise. For this article, I created a SaaS account. App dynamics will send the account details to your registered e-mail address. Because the SaaS option was selected during sign up, the e-mail will include an SaaS URL and the account name. Now, click the SaaS URL and enter your account name. Once account number is keyed in, it will hide the rest of the fields and only the login button is enabled. Click the login button and enter the username/password to complete the sign-up process. Now, you will be redirected to the dashboard screen.
Once logged in, from the displayed tabs, select "User Experience" and then select "Mobile Apps," as shown in Figure 1.
Figure 1: The Mobile Apps tab
Now, click "Add App." The screen now displays a drop-down to select "Android" or "iOS." First, click iOS. It displays a list of steps. Go directly to the second step to create a new mobile app group and click Continue. This generates an application key that we will use in our Xamarin project. The new mobile app group can be shared between both iOS and Android; we will use the same key for Android, too.
Create a Xamarin Forms project with target platforms as Android and iOS. Add a NuHet package named "AppDynamics.Agent" on both Xamarin forms and the target projects: Android and iOS projects.
In the iOS project, open "AppDelegate.cs." In the FinishedLaunching method, add the following code to establish the connection between the app and the AppDynamics SaaS.
var config = AppDynamics.Agent.AgentConfiguration.Create ("Application key"); config.LoggingLevel = AppDynamics.Agent.LoggingLevel.Error; AppDynamics.Agent.Instrumentation.InitWithConfiguration(config);
Similarly, in the Android project, open the MainActivity.cs and, in the OnCreate method, add the same lines of code. These lines of code initialize the app dynamics agent. Add this code before the loading the App code:
The next step is to add the logging to the code to track the events, messages, errors, and other required information. In my sample project, I have the first page, which is a login page. I will trace the incorrect login and track it on the app dynamics.
In the login_Clicked method, I add the following code:
var callTracker = AppDynamics.Agent.Instrumentation.BeginCall ("className", "MethodName", "Login Clicked");
As the name suggests, it starts the logging. Then, you can add a breadcrumb for all the important steps. To trace a session or an error scenario, set BreadCrumbVisibilty to CrashesAndSessions.
Finally, we end the trace call with the following code:
1. AppDynamics.Agent.Instrumentation.EndCall(callTracker); 2. AppDynamics.Agent.Instrumentation.EndCall(callTracker, ex);
EndCall can include an exception object as depicted in Line 2 or just end the trace without any exception.
Now, if you navigate to the SaaS site, you will see the trace logged for iOS and Android, separately.
Once a mobile app is published, it's difficult to troubleshoot the error and also the steps and the data that was in reference when the event occurred. In such scenarios, it helps to have a centralized logging with the details. Appdynamics captures user phone information such as the model, OS version, and so forth. App dynamics tracks the user's session, time of login, and many other details. We can add instrumentation to track any detail we need. Further analysis can be performed on the Web site. In Appdynamics, we also can enable capturing the screenshot.