mirror of https://github.com/abpframework/abp.git
7 changed files with 142 additions and 7 deletions
@ -0,0 +1,17 @@ |
|||
using System.Diagnostics.CodeAnalysis; |
|||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; |
|||
using Volo.Abp; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public static class AbpWebAssemblyServiceCollectionExtensions |
|||
{ |
|||
public static WebAssemblyHostBuilder GetHostBuilder( |
|||
[NotNull] this IServiceCollection services) |
|||
{ |
|||
Check.NotNull(services, nameof(services)); |
|||
|
|||
return services.GetSingletonInstance<WebAssemblyHostBuilder>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly.ExceptionHandling |
|||
{ |
|||
public class AbpExceptionHandlingLogger : ILogger, IDisposable |
|||
{ |
|||
private readonly IServiceCollection _serviceCollection; |
|||
private IServiceScope _serviceScope; |
|||
private IUiMessageService _messageService; |
|||
|
|||
public AbpExceptionHandlingLogger(IServiceCollection serviceCollection) |
|||
{ |
|||
_serviceCollection = serviceCollection; |
|||
} |
|||
|
|||
public void Log<TState>( |
|||
LogLevel logLevel, |
|||
EventId eventId, |
|||
TState state, |
|||
Exception exception, |
|||
Func<TState, Exception, string> formatter) |
|||
{ |
|||
if (logLevel != LogLevel.Critical && logLevel != LogLevel.Error) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
TryInitialize(); |
|||
|
|||
if (_messageService == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
//TODO: handle exception types
|
|||
_messageService.ErrorAsync( |
|||
exception?.Message ?? state?.ToString() ?? "Unknown error!" |
|||
); |
|||
} |
|||
|
|||
private void TryInitialize() |
|||
{ |
|||
var serviceProvider = _serviceCollection.GetServiceProviderOrNull(); |
|||
if (serviceProvider == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
_serviceScope = serviceProvider.CreateScope(); |
|||
_messageService = _serviceScope.ServiceProvider.GetRequiredService<IUiMessageService>(); |
|||
} |
|||
|
|||
public bool IsEnabled(LogLevel logLevel) |
|||
{ |
|||
return logLevel == LogLevel.Critical || logLevel == LogLevel.Error; |
|||
} |
|||
|
|||
public IDisposable BeginScope<TState>(TState state) |
|||
{ |
|||
return NullDisposable.Instance; |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
_serviceScope?.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly.ExceptionHandling |
|||
{ |
|||
public class AbpExceptionHandlingLoggerProvider : ILoggerProvider |
|||
{ |
|||
private AbpExceptionHandlingLogger _logger; |
|||
private static readonly object SyncObj = new object(); |
|||
private readonly IServiceCollection _serviceCollection; |
|||
|
|||
public AbpExceptionHandlingLoggerProvider(IServiceCollection serviceCollection) |
|||
{ |
|||
_serviceCollection = serviceCollection; |
|||
} |
|||
|
|||
public ILogger CreateLogger(string categoryName) |
|||
{ |
|||
if (_logger == null) |
|||
{ |
|||
lock (SyncObj) |
|||
{ |
|||
if (_logger == null) |
|||
{ |
|||
_logger = new AbpExceptionHandlingLogger(_serviceCollection); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return _logger; |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
_logger.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue