Skip to content

Commit

Permalink
update Logging Prism 8 beta
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Aug 27, 2020
1 parent ce8a0f3 commit 4104fb6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/Prism.Forms.Extended/Ioc/RequiredTypesExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Prism.AppModel;
Expand All @@ -20,7 +21,9 @@ public static class RequiredTypesExtensions

public static void RegisterRequiredTypes(this IContainerRegistry containerRegistry)
{
containerRegistry.RegisterManySingletonOnce<AggregateLogger>(typeof(IAggregateLogger), typeof(ILogger), typeof(IAnalyticsService), typeof(ICrashesService));
if (!containerRegistry.IsRegistered<IAggregateLogger>())
containerRegistry.UseAggregateLogger();

containerRegistry.RegisterSingletonOnce<IApplicationProvider, ApplicationProvider>();
containerRegistry.RegisterSingletonOnce<IApplicationStore, ApplicationStore>();
containerRegistry.RegisterSingletonOnce<IEventAggregator, EventAggregator>();
Expand All @@ -34,34 +37,38 @@ public static void RegisterRequiredTypes(this IContainerRegistry containerRegist
containerRegistry.RegisterSingletonOnce<IDialogService, DialogService>();
containerRegistry.Register<INavigationService, ErrorReportingNavigationService>(NavigationServiceName);
containerRegistry.RegisterScoped<INavigationService, ErrorReportingNavigationService>();
var isRegistered = containerRegistry.IsRegistered<INavigationService>(NavigationServiceName);
}

public static void RegisterPrismCoreServices(this IServiceCollection services)
{
services.RegisterSingletonIfNotRegistered<IAggregableLogger, ConsoleLoggingService>();
services.RegisterSingletonIfNotRegistered<IAnalyticsService>(sp => (IAggregableLogger)sp.GetService(typeof(IAggregableLogger)));
services.RegisterSingletonIfNotRegistered<ICrashesService>(sp => (IAggregableLogger)sp.GetService(typeof(IAggregableLogger)));
services.RegisterSingletonIfNotRegistered<ILogger>(sp => (IAggregableLogger)sp.GetService(typeof(IAggregableLogger)));
if(!services.IsRegistered<IAggregateLogger>())
{
services.AddSingleton<AggregateLogger>(p =>
{
var logger = new AggregateLogger();
logger.AddLoggers(p.GetService<IEnumerable<IAggregableLogger>>());
return logger;
});
services.AddTransient<IAggregateLogger>(p => p.GetRequiredService<AggregateLogger>());
services.AddTransient<ILogger>(p => p.GetRequiredService<AggregateLogger>());
services.AddTransient<IAnalyticsService>(p => p.GetRequiredService<AggregateLogger>());
services.AddTransient<ICrashesService>(p => p.GetRequiredService<AggregateLogger>());
}
services.RegisterSingletonIfNotRegistered<IEventAggregator, EventAggregator>();
services.RegisterSingletonIfNotRegistered<IModuleCatalog, ModuleCatalog>();
services.RegisterSingletonIfNotRegistered<IModuleManager, ModuleManager>();
services.RegisterSingletonIfNotRegistered<IModuleInitializer, ModuleInitializer>();
}

private static bool IsRegistered<T>(this IServiceCollection services) =>
services.Any(x => x.ServiceType == typeof(T));

private static void RegisterSingletonIfNotRegistered<T, TImpl>(this IServiceCollection services)
where T : class
where TImpl : class, T
{
if (!services.Any(s => s.ServiceType == typeof(T)))
services.AddSingleton<T, TImpl>();
}

private static void RegisterSingletonIfNotRegistered<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory)
where T : class
{
if (!services.Any(s => s.ServiceType == typeof(T)))
services.AddSingleton<T>(implementationFactory);
}
}
}
2 changes: 1 addition & 1 deletion src/Prism.Forms.Extended/Prism.Forms.Extended.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<ItemGroup>
<PackageReference Include="Prism.Forms" Version="8.0.0.1850-pre" />
<PackageReference Include="Prism.Plugin.Logging.Abstractions" Version="8.0.9-beta" />
<PackageReference Include="Prism.Plugin.Logging.Abstractions" Version="8.0.11-beta" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 4104fb6

Please sign in to comment.