Cross Platform
Android
iOS
Mac
Test Cloud

Android Debug Log

One very common trick developers use to debug their applications is using Console.WriteLine. However, on a mobile platform like Android there is no console. Android devices provides a log that you will likely need to utilize while writing apps. This is sometimes referred to as "logcat" due to the command typed to retrieve it.

Accessing from Visual Studio

Xamarin.Android has integrated the Android debug log into Visual Studio to provide a graphical way to get the log. Use View > Other Windows > Android Device Logging. An example output is shown below:

  • Click the device icon to choose the device's log to view.
  • Click the refresh icon to refresh the log.

Accessing from the Command Line

Another option to view the debug log is via the command line. Open a console window and navigate to the Android SDK platform-tools folder (like C:\android-sdk-windows\platform-tools).

If you only have one device attached, you can view the log with:

adb logcat

If you have more than one device attached, you need to specify which device log to view. For example adb -d logcat shows the log of the only physical device connected, while adb -e logcat shows the log of the only emulator running.

More commands can be found by just running "adb".

Writing to the Debug Log

Now you can view the log, what about writing to the log? This is done via Android.Util.Log:

string tag = "myapp";

Log.Info (tag, "this is an info message");
Log.Warn (tag, "this is a warning message");
Log.Error (tag, "this is an error message");

This produces:

I/myapp   (11103): this is an info message
W/myapp   (11103): this is a warning message
E/myapp   (11103): this is an error message

Interesting Messages

When reading the log, and especially when providing log snippets to others (as providing the full log file is (1) overkill, and (2) unreadable), the most important line to start with is a line resembling:

I/ActivityManager(12944): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=GcTest.GcTest/gctest.Activity1 } from pid 24175

Specfically, a line matching the regular expression:

^I.*ActivityManager.*Starting: Intent

and containing the name of your application package. This is the line which corresponds to the start of your activity, and most (but not all) of the following messages should relate to your application.

In particular, every message contains the process identifier (pid) of the process generating the message. In the above ActivityManager message, process 12944 generated the message. To determine which process is your process, look for the mono.MonoRuntimeProvider message:

I/ActivityThread(  602): Pub TouchTest.TouchTest.__mono_init__: mono.MonoRuntimeProvider

This message comes from the process that was started, and all following messages that contain the same pid come from the same process.