Skip to content

Commit

Permalink
merge results
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi committed Mar 29, 2024
2 parents c1f10ac + 085f663 commit 3077217
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//-:cnd:noEmit
using System.Diagnostics;
using System.Text;
using Microsoft.Extensions.Logging;

namespace Boilerplate.Client.Core.Services;

Expand All @@ -8,8 +10,21 @@ public abstract partial class ExceptionHandlerBase : IExceptionHandler
[AutoInject] protected readonly IStringLocalizer<AppStrings> Localizer = default!;
[AutoInject] protected readonly MessageBoxService MessageBoxService = default!;
[AutoInject] protected Bit.Butil.Console Console = default!;
[AutoInject] protected ILogger<ExceptionHandlerBase> Logger = default!;

public virtual void Handle(Exception exception, IDictionary<string, object?>? parameters = null)
public void Handle(Exception exp, IDictionary<string, object?>? parameters = null)
{
if (exp is TaskCanceledException)
{
return;
}

parameters ??= new Dictionary<string, object?>();

Handle(exp, parameters.ToDictionary(i => i.Key, i => i.Value ?? string.Empty));
}

protected virtual void Handle(Exception exception, Dictionary<string, object> parameters)
{
var isDebug = BuildConfiguration.IsDebug();

Expand All @@ -18,17 +33,24 @@ public virtual void Handle(Exception exception, IDictionary<string, object?>? 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)]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<PackageReference Include="Oscore.Maui.Android.InAppUpdates" Version="1.1.0" />
<PackageReference Include="Oscore.Maui.AppStoreInfo" Version="1.0.7" />
<PackageReference Include="Oscore.Maui.InAppReviews" Version="1.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="8.0.0" />
</ItemGroup>

<!-- Build Properties must be defined within these property groups to ensure successful publishing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Boilerplate.Client.Maui.Services;
using Microsoft.Extensions.Logging;

namespace Boilerplate.Client.Maui;

Expand Down Expand Up @@ -30,6 +31,16 @@ public static void ConfigureServices(this MauiAppBuilder builder)
return httpClient;
});

services.AddLogging(logginBuildr =>
{
logginBuildr.AddDebug();
if (OperatingSystem.IsWindows())
{
logginBuildr.AddEventLog();
}
logginBuildr.AddEventSourceLogger();
});

services.TryAddTransient<MainPage>();
services.TryAddTransient<IStorageService, MauiStorageService>();
services.TryAddSingleton<IBitDeviceCoordinator, MauiDeviceCoordinator>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
namespace Boilerplate.Client.Maui.Services;

namespace Boilerplate.Client.Maui.Services;

/// <summary>
/// You can easily install AppCenter, Firebase Crashlytics, and other exception tracking libraries in your Client.Maui project.
/// Then, you can use their APIs to monitor all exceptions across Android, iOS, Windows, and macOS.
/// </summary>
public partial class MauiExceptionHandler : ExceptionHandlerBase
{
public override void Handle(Exception exception, IDictionary<string, object?>? parameters = null)
protected override void Handle(Exception exception, Dictionary<string, object> parameters)
{
if (exception is TaskCanceledException)
{
return;
}

base.Handle(exception, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

public partial class WebExceptionHandler : ExceptionHandlerBase
{
public override void Handle(Exception exception, IDictionary<string, object?>? parameters = null)
protected override void Handle(Exception exception, Dictionary<string, object> parameters)
{
if (exception is TaskCanceledException)
{
return;
}

base.Handle(exception, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

<PackageReference Include="Bit.CodeAnalyzers" Version="8.8.0-pre-03" PrivateAssets="all" />
<PackageReference Include="Bit.SourceGenerators" Version="8.8.0-pre-03" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Wpf" Version="8.0.14" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2420.47" />
<PackageReference Include="Velopack" Version="0.0.359" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Wpf" Version="8.0.14" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="8.0.0" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2420.47" />
<PackageReference Include="Velopack" Version="0.0.359" />

<Content Include="..\Boilerplate.Client.Maui\wwwroot\index.html" Link="wwwroot\index.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.Http;
using Boilerplate.Client.Windows.Services;
using Microsoft.Extensions.Logging;

namespace Boilerplate.Client.Windows;

Expand Down Expand Up @@ -35,6 +36,13 @@ public static void ConfigureServices(this IServiceCollection services)
services.TryAddTransient<IBitDeviceCoordinator, WindowsDeviceCoordinator>();
services.TryAddTransient<IExceptionHandler, WindowsExceptionHandler>();

services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddEventLog();
loggingBuilder.AddEventSourceLogger();
loggingBuilder.AddDebug();
});

services.AddClientCoreProjectServices();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ namespace Boilerplate.Client.Windows.Services;

public partial class WindowsExceptionHandler : ExceptionHandlerBase
{
public override void Handle(Exception exception, IDictionary<string, object?>? parameters = null)
protected override void Handle(Exception exception, Dictionary<string, object> parameters)
{
if (exception is TaskCanceledException)
{
return;
}

base.Handle(exception, parameters);
}
}

0 comments on commit 3077217

Please sign in to comment.