From ffebbfe2b09c40820287c908c26be84abc3374c2 Mon Sep 17 00:00:00 2001 From: ysmoradi Date: Fri, 29 Mar 2024 05:27:58 +0100 Subject: [PATCH 1/5] Configure MS.Ext.Logging in Boilerplate project template --- .../Services/ExceptionHandlerBase.cs | 24 ++++++++++++++----- .../Boilerplate.Client.Maui.csproj | 3 +++ .../MauiProgram.Services.cs | 11 +++++++++ .../Boilerplate.Client.Windows.csproj | 3 +++ .../Program.Services.cs | 8 +++++++ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ExceptionHandlerBase.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ExceptionHandlerBase.cs index 77ed5c4e77..817788d773 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ExceptionHandlerBase.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ExceptionHandlerBase.cs @@ -1,5 +1,7 @@ //-:cnd:noEmit using System.Diagnostics; +using System.Text; +using Microsoft.Extensions.Logging; namespace Boilerplate.Client.Core.Services; @@ -8,9 +10,12 @@ public abstract partial class ExceptionHandlerBase : IExceptionHandler [AutoInject] protected readonly IStringLocalizer Localizer = default!; [AutoInject] protected readonly MessageBoxService MessageBoxService = default!; [AutoInject] protected Bit.Butil.Console Console = default!; + [AutoInject] protected ILogger Logger = default!; public virtual void Handle(Exception exception, IDictionary? parameters = null) { + parameters ??= new Dictionary(); + var isDebug = BuildConfiguration.IsDebug(); string exceptionMessage = (exception as KnownException)?.Message ?? @@ -18,17 +23,24 @@ public virtual void Handle(Exception exception, IDictionary? pa if (isDebug) { - if (OperatingSystem.IsBrowser() || AppRenderMode.IsBlazorHybrid) - { - _ = Console.Error(exceptionMessage); - } - else + if (AppRenderMode.IsBlazorHybrid) { - _ = System.Console.Out.WriteLineAsync(exceptionMessage); + StringBuilder errorInfo = new(); + errorInfo.AppendLine(exceptionMessage); + foreach (var item in parameters) + { + errorInfo.AppendLine($"{item.Key}: {item.Value}"); + } + _ = Console.Error(errorInfo.ToString()); } Debugger.Break(); } + using (var scope = Logger.BeginScope(parameters.ToDictionary(i => i.Key, i => i.Value ?? string.Empty))) + { + Logger.LogError(exception, exceptionMessage); + } + _ = MessageBoxService.Show(exceptionMessage, Localizer[nameof(AppStrings.Error)]); } } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Boilerplate.Client.Maui.csproj b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Boilerplate.Client.Maui.csproj index 380f010172..061d8202cf 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Boilerplate.Client.Maui.csproj +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Boilerplate.Client.Maui.csproj @@ -103,6 +103,9 @@ + + +