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..1074031b5d 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,8 +10,21 @@ 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) + public void Handle(Exception exp, IDictionary? parameters = null) + { + if (exp is TaskCanceledException) + { + return; + } + + parameters ??= new Dictionary(); + + Handle(exp, parameters.ToDictionary(i => i.Key, i => i.Value ?? string.Empty)); + } + + protected virtual void Handle(Exception exception, Dictionary parameters) { var isDebug = BuildConfiguration.IsDebug(); @@ -18,17 +33,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 3b9a78595b..e2ef259792 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 @@ + + +