41

This might be a nit-picky thing, but in Xamarin when running an Android app, it dumps tons of lines in the console that start with [Mono]

Is there any way to disable these logs?

Thanks in advance

julianwyz
  • 3,104
  • 1
  • 29
  • 34

2 Answers2

78

This can be done by changing the state of Monos execution environment on the device; which is just a set of environment variables that alters Monos behaviour (be it garbage collection, logging etc). In this case, to alter the logging behavior we need to modify the values stored in the environment variables MONO_LOG_LEVEL and MONO_LOG_MASK.

Xamarin.Android offers 2 mechanisms developers can use to change the execution environment:

  • 1. Using adb shell setprop debug.mono.env. This can be done as a post build action.
  • 2. Using an environment build file to change the execution environment state per project.

I prefer to use method 2 as it's easier to edit a text file than changing build actions. Do this using the steps outlined below.

Adding An Environment File

Add a plain text file called environment.txt to the root path of your Xamarin.Android project. enter image description here

Right click on environment.txt and set its build action to AndroidEnvironment.

The environment file is series of key=value pairs seperated by newlines. For logging, we can set the following variables:

MONO_LOG_LEVEL

  • debug
  • info
  • message
  • warning
  • critical
  • error

MONO_LOG_MASK

  • asm
  • dll
  • cfg
  • all
  • type
  • gc

For example, we can ignore most messages by filtering MONO_LOG_LEVEL by error:

environment.txt

MONO_LOG_LEVEL=error

Background reading:

matthewrdev
  • 11,930
  • 5
  • 52
  • 64
  • Thanks so much, Matt! Answers my question and then some :) – julianwyz Nov 05 '14 at 23:08
  • Pleasure :) FYI, this file is also used to control which garbage collection algorithm is used by Mono. You can use `MONO_GC_PARAMS=bridge-implementation=##` to experiment with the different algorithms if your app is thrashing the GC and becoming non-responsive as a result. – matthewrdev Nov 05 '14 at 23:13
  • 1
    I did all this but am not sure if it's changing anything. How do I know that these parameters are being compiled in properly? – jjxtra Jul 11 '15 at 16:25
  • @PsychoDad, for the log level filtering, most of the `mono-stdout` logging will vanish. For the GC implementation, the garbage collection message will show something like `GC_OLD`, `GC_NEW` or `GC_TARJAN` each type a collection is triggered. – matthewrdev Jul 12 '15 at 07:52
  • 6
    @matthewrobbinsdev I must be doing something wrong. I've created the file and set the build action to AndroidEnvironment but I don't think it's picking it up. I can out gibberish in the file and I get no build errors. I guess I can email Xamarin support. – jjxtra Jul 12 '15 at 15:13
  • 3
    All these years of suffering, finally had the clever idea of googling about it :)) Thx so much Matt! – George B Jun 28 '19 at 12:08
  • @jjxtra Did you ever get this to work? Have the same issue right now – rumblefx0 Mar 29 '23 at 13:23
  • 1
    No, stopped using Xamarin years ago – jjxtra Mar 29 '23 at 14:42
  • @matthewrdev Did this, but appears to have no effect - any thoughts what could be wrong? Do I perhaps need to restart ADB server, VS or the emulator? – rumblefx0 Apr 07 '23 at 10:57
2

What I do is below. It's still imperfect as the window holds 10k logs, including the hidden ones, so mine eventually disappear. Additionally, I can't seem to copy from the window.

1) Use a tag of "AAA" for all my logs.
2) View the output in Tools>Android>Device Log
3) Sort alphabetically by Tag.
4) I find that the outputs beneath mine are still distracting. I can click the "filter" for the Tag and uncheck everything but mine. Annoyingly, I have to repeat this step periodically as new tags are not filtered by default.

enter image description here

William Jockusch
  • 26,513
  • 49
  • 182
  • 323