From 5ca46603dae30d7f7735295354951a7b8adf057f Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 5 Sep 2023 18:39:12 +0200 Subject: [PATCH 1/6] Add a target to run application with logging We are frequently faced with the need to look at some detailed information optionally logged by our runtime (as well as by the MonoVM runtime in dotnet) and in order to obtain the necessary data, we ask our users to perform a series of steps on command line. This is a task we can easily wrap in a nice target, so that instead of having to type 5 lines of commands the user invokes a single command which will do the rest for them: dotnet build -t:RunWithLogging The above command will set the `debug.mono.log` property to the desired value, increase logcat buffer, clear the buffer, start the application waiting for it to be fully started, then pause for 1s and dump the logcat buffer to a file. Optionally, the `$(_RunLogVerbose)` property can be set to `true` in order to log verbose MonoVM data and the `$(_RunLogFilePath)` can be set to a logcat output file path (defaults to `$(IntermediateOutputPath)/logcat.txt`) --- build-tools/scripts/TestApks.targets | 8 ++-- .../Sleep.cs | 12 +++--- .../Microsoft.Android.Sdk.Application.targets | 40 +++++++++++++++++++ .../Xamarin.Android.Build.Tasks.csproj | 3 ++ 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/build-tools/scripts/TestApks.targets b/build-tools/scripts/TestApks.targets index 4592c83a885..484bf5eadde 100644 --- a/build-tools/scripts/TestApks.targets +++ b/build-tools/scripts/TestApks.targets @@ -12,7 +12,7 @@ - + @@ -160,7 +160,7 @@ ContinueOnError="WarnAndContinue" Command="kill -HUP $(_EmuPid)" /> - @@ -375,7 +375,7 @@ ContinueOnError="true"> - @@ -386,7 +386,7 @@ Condition=" '$(_SdkManagerExitCode)' != '0' "> - diff --git a/build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/Sleep.cs b/build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/Sleep.cs index 93da4a74661..37c04e189fa 100644 --- a/build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/Sleep.cs +++ b/build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/Sleep.cs @@ -3,19 +3,21 @@ using Microsoft.Build.Framework; using Microsoft.Build.Utilities; +using Microsoft.Android.Build.Tasks; + +using TPLTask = System.Threading.Tasks.Task; namespace Xamarin.Android.BuildTools.PrepTasks { - public class Sleep : Task + public class XASleepInternal : AndroidAsyncTask { + public override string TaskPrefix => "XASI"; public int Milliseconds { get; set; } - public override bool Execute () + public override TPLTask RunTaskAsync () { Log.LogMessage (MessageImportance.Normal, $"Going to sleep for {Milliseconds}ms"); - Thread.Sleep (Milliseconds); - - return true; + return TPLTask.Delay (Milliseconds, CancellationToken); } } } diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets index 96dfb705f95..4729e00c124 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets @@ -7,6 +7,8 @@ This file contains targets specific for Android application projects. *********************************************************************************************** --> + + false @@ -28,4 +30,42 @@ This file contains targets specific for Android application projects. + + <_RunWithLoggingDependsOn> + Install; + + + <_RunLogFilePath Condition=" '$(_RunLogFilePath)' == '' ">$(IntermediateOutputPath)logcat.txt + <_RunLogVerbose Condition=" '$(_RunLogVerbose)' == '' ">false + + + + + + + + + <_MonoLog>default,assembly,timing=bare + <_MonoLog Condition=" '$(_RunLogVerbose)' != 'false' ">$(_MonoLog),mono_log_level=debug,mono_log_mask=all + <_SleepTime>1000 + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj index 3003ed96c68..c80b4722758 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj @@ -312,6 +312,9 @@ Mono.Android\NativeLibraryReferenceAttribute.cs + + Xamarin.Android.BuildTools.PrepTasks\Sleep.cs + <_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.LaunchMode.cs" /> <_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.ScreenOrientation.cs" /> <_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.ConfigChanges.cs" /> From eeecc89dc38226005d1a56924b4d7b7167003286 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 5 Sep 2023 19:24:38 +0200 Subject: [PATCH 2/6] Reference xamarin-android-tools --- build-tools/xa-prep-tasks/xa-prep-tasks.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build-tools/xa-prep-tasks/xa-prep-tasks.csproj b/build-tools/xa-prep-tasks/xa-prep-tasks.csproj index d9c6ac53885..4cb84d00174 100644 --- a/build-tools/xa-prep-tasks/xa-prep-tasks.csproj +++ b/build-tools/xa-prep-tasks/xa-prep-tasks.csproj @@ -9,4 +9,8 @@ + + + + From 018ff97902a7119b7bed15a91da42a04327363da Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 5 Sep 2023 19:30:47 +0200 Subject: [PATCH 3/6] Add logs --- Documentation/workflow/DevelopmentTips.md | 18 ++++++++++++++++++ .../Microsoft.Android.Sdk.Application.targets | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Documentation/workflow/DevelopmentTips.md b/Documentation/workflow/DevelopmentTips.md index 68f96dc1ac0..04bda8fdcee 100644 --- a/Documentation/workflow/DevelopmentTips.md +++ b/Documentation/workflow/DevelopmentTips.md @@ -477,6 +477,24 @@ automatically picked up by the .NET SDK. ## Enabling Mono Logging +### The easy way + +A quick way to enable Mono logging is to use the `RunWithLogging` +target: + +```bash +$ dotnet build -t:RunWithLogging +``` + +If successful, messages printed to the screen will show location +of the logcat file with the logged messages. + +Verbosity of logging can be increased by setting the `$(RunLogVerbose)` +property to `true`, in which case the log output file will contain +(very) verbose log messages from the MonoVM runtime. + +### The manual way + Since [6e58ce4][6e58ce4], logging from Mono is no longer enabled by default. You can set the `debug.mono.log` system property to answer questions like: Is AOT working? Is the Mono Interpreter enabled? diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets index 4729e00c124..7efd5aa8314 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets @@ -36,7 +36,7 @@ This file contains targets specific for Android application projects. <_RunLogFilePath Condition=" '$(_RunLogFilePath)' == '' ">$(IntermediateOutputPath)logcat.txt - <_RunLogVerbose Condition=" '$(_RunLogVerbose)' == '' ">false + false <_MonoLog>default,assembly,timing=bare - <_MonoLog Condition=" '$(_RunLogVerbose)' != 'false' ">$(_MonoLog),mono_log_level=debug,mono_log_mask=all + <_MonoLog Condition=" '$(RunLogVerbose)' != 'false' ">$(_MonoLog),mono_log_level=debug,mono_log_mask=all <_SleepTime>1000 From 10ce66e3adc20fc99896957c0230e15b6e4dbadc Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 5 Sep 2023 19:38:36 +0200 Subject: [PATCH 4/6] more docs and rename the timeout property --- Documentation/workflow/DevelopmentTips.md | 7 +++++++ .../targets/Microsoft.Android.Sdk.Application.targets | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Documentation/workflow/DevelopmentTips.md b/Documentation/workflow/DevelopmentTips.md index 04bda8fdcee..6e28553ff2e 100644 --- a/Documentation/workflow/DevelopmentTips.md +++ b/Documentation/workflow/DevelopmentTips.md @@ -493,6 +493,13 @@ Verbosity of logging can be increased by setting the `$(RunLogVerbose)` property to `true`, in which case the log output file will contain (very) verbose log messages from the MonoVM runtime. +By default, the target will wait for a 1000ms before it dumps the +logcat buffer to file. This is to give the Android logging daemon +time to actually put all the messages logged by the application in +the logcat buffer. This value can be overridden by setting the +`$(RunLogDelay)` MSBuild property to a number of milliseconds that +the target should wait before creating the log file. + ### The manual way Since [6e58ce4][6e58ce4], logging from Mono is no longer enabled by diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets index 7efd5aa8314..e3e6c51f769 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets @@ -50,7 +50,7 @@ This file contains targets specific for Android application projects. <_MonoLog>default,assembly,timing=bare <_MonoLog Condition=" '$(RunLogVerbose)' != 'false' ">$(_MonoLog),mono_log_level=debug,mono_log_mask=all - <_SleepTime>1000 + 1000 @@ -62,8 +62,8 @@ This file contains targets specific for Android application projects. - - + + From ae7e18215351e18d95a68d2a372e9bb6fa9c7d45 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 5 Sep 2023 21:11:22 +0200 Subject: [PATCH 5/6] More docs --- Documentation/guides/building-apps/build-targets.md | 12 ++++++++++++ Documentation/workflow/DevelopmentTips.md | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Documentation/guides/building-apps/build-targets.md b/Documentation/guides/building-apps/build-targets.md index ebd2c96d5fa..b41b0ebb371 100644 --- a/Documentation/guides/building-apps/build-targets.md +++ b/Documentation/guides/building-apps/build-targets.md @@ -96,6 +96,18 @@ MSBuild property controls which [Visual Studio SDK Manager repository](~/android/get-started/installation/android-sdk.md?tabs=windows#repository-selection) is used for package name and package version detection, and URLs to download. +## RunWithLogging + +Runs the application with additional logging enabled. Helpful when reporting or investigating an issue with +either the application or the runtime. If successful, messages printed to the screen will show location +of the logcat file with the logged messages. + +Properties which affect how the target works: + + * `/p:RunLogVerbose=true` enables even more verbose logging from MonoVM + * `/p:RunLogDelay=X` where `X` should be replaced with time in milliseconds to wait before writing the + log output to file. Defaults to `1000`. + ## SignAndroidPackage Creates and signs the Android package (`.apk`) file. diff --git a/Documentation/workflow/DevelopmentTips.md b/Documentation/workflow/DevelopmentTips.md index 6e28553ff2e..20d8d494426 100644 --- a/Documentation/workflow/DevelopmentTips.md +++ b/Documentation/workflow/DevelopmentTips.md @@ -452,7 +452,7 @@ A second (better) way is to add this MSBuild target to your Android ```xml - Date: Tue, 5 Sep 2023 21:20:30 +0200 Subject: [PATCH 6/6] Address feedback --- Documentation/guides/building-apps/build-targets.md | 2 +- Documentation/workflow/DevelopmentTips.md | 2 +- .../targets/Microsoft.Android.Sdk.Application.targets | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Documentation/guides/building-apps/build-targets.md b/Documentation/guides/building-apps/build-targets.md index b41b0ebb371..fc6697ce83f 100644 --- a/Documentation/guides/building-apps/build-targets.md +++ b/Documentation/guides/building-apps/build-targets.md @@ -105,7 +105,7 @@ of the logcat file with the logged messages. Properties which affect how the target works: * `/p:RunLogVerbose=true` enables even more verbose logging from MonoVM - * `/p:RunLogDelay=X` where `X` should be replaced with time in milliseconds to wait before writing the + * `/p:RunLogDelayInMS=X` where `X` should be replaced with time in milliseconds to wait before writing the log output to file. Defaults to `1000`. ## SignAndroidPackage diff --git a/Documentation/workflow/DevelopmentTips.md b/Documentation/workflow/DevelopmentTips.md index 20d8d494426..1ea7c172781 100644 --- a/Documentation/workflow/DevelopmentTips.md +++ b/Documentation/workflow/DevelopmentTips.md @@ -497,7 +497,7 @@ By default, the target will wait for a 1000ms before it dumps the logcat buffer to file. This is to give the Android logging daemon time to actually put all the messages logged by the application in the logcat buffer. This value can be overridden by setting the -`$(RunLogDelay)` MSBuild property to a number of milliseconds that +`$(RunLogDelayInMS)` MSBuild property to a number of milliseconds that the target should wait before creating the log file. ### The manual way diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets index e3e6c51f769..4e03d35f525 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets @@ -50,7 +50,7 @@ This file contains targets specific for Android application projects. <_MonoLog>default,assembly,timing=bare <_MonoLog Condition=" '$(RunLogVerbose)' != 'false' ">$(_MonoLog),mono_log_level=debug,mono_log_mask=all - 1000 + 1000 @@ -62,8 +62,8 @@ This file contains targets specific for Android application projects. - - + +