From 791fedb57c47eba5356c483ca8ab492623d094f1 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Thu, 21 Sep 2023 21:49:11 +0000 Subject: [PATCH] [Xamarin.Android.Build.Tasks] Add `RunWithLogging` target (#8324) 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/runtime), and in order to obtain the necessary data, we ask our users to perform a series of steps on the 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 instead invokes a single command which will do the rest for them: dotnet build -t:RunWithLogging The above command will: 1. Set the `debug.mono.log` Android system property 2. increase the `adb logcat` buffer size 3. clear the `adb logcat` buffer, 4. start the application waiting for it to be fully started 5. pause for 1s 6. dump the `adb logcat` buffer to a file. 7. Print a message mentioning where the `adb logcat` output is. The `$(RunLogVerbose)` MSBuild property can be used to increase verbosity. By default the `RunWithLogging` target will set the `debug.mono.log` system property to: default,assembly,timing=bare When `$(RunLogVerbose)`=true, `debug.mono.log` is instead set to: default,assembly,timing=bare,mono_log_level=debug,mono_log_mask=all The `$(RunLogDelayInMS)` MSBuild property can be used to override the pause in step (5). The `$(_RunLogFilePath)` MSBuild property controls the output filename; by default it is `$(IntermediateOutputPath)logcat.txt`. --- .../guides/building-apps/build-targets.md | 12 ++++++ Documentation/workflow/DevelopmentTips.md | 27 ++++++++++++- build-tools/scripts/TestApks.targets | 8 ++-- .../Sleep.cs | 12 +++--- .../xa-prep-tasks/xa-prep-tasks.csproj | 4 ++ .../Microsoft.Android.Sdk.Application.targets | 40 +++++++++++++++++++ .../Xamarin.Android.Build.Tasks.csproj | 3 ++ 7 files changed, 96 insertions(+), 10 deletions(-) diff --git a/Documentation/guides/building-apps/build-targets.md b/Documentation/guides/building-apps/build-targets.md index ebd2c96d5fa..fc6697ce83f 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:RunLogDelayInMS=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 68f96dc1ac0..1ea7c172781 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 - - + @@ -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/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 @@ + + + + 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..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 @@ -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 + false + + + + + + + + + <_MonoLog>default,assembly,timing=bare + <_MonoLog Condition=" '$(RunLogVerbose)' != 'false' ">$(_MonoLog),mono_log_level=debug,mono_log_mask=all + 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" />