Android Debug Log

PDF for offline use:

Let us know how you feel about this.


0/250
Thanks for the feedback!

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

In Visual Studio 2015 and above the Android and iOS log pads are unified

Visual Studio 2015

The new Device Log tool window for VS allows to show logs for Android and iOS devices. It can be shown by executing any of the following commands:

  • View -> Other Windows -> Device Log
  • Tools -> Android -> Device Log
  • Android toolbar -> Device Log

Once the tool window is displayed, you can select the (physical) device from the devices combo box. When the device is selected, it automatically starts to add log entries from a running app in the table. Switching between devices will stop and start the device logging.In order for the devices to appear in the combobox, an Android project must be loaded. If the device does not appear in the combobox, check first if it’s available in the Start Debug drop down.

This tool window provides: a table of log entries, a combo box for device selection, a way to clear log entries, a search box, and play/stop/pause buttons.

Visual Studio 2012 & 2013

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.