Browse Source

Make IInitLogger inherit ILogger.

pull/7972/head
maliming 6 years ago
parent
commit
460e38f654
  1. 9
      framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLoggingExtensions.cs
  2. 18
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs
  3. 6
      framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs
  4. 30
      framework/src/Volo.Abp.Core/Volo/Abp/Logging/AbpInitLogEntry.cs
  5. 30
      framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLogger.cs
  6. 12
      framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLogger.cs
  7. 7
      framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLoggerFactory.cs
  8. 15
      framework/src/Volo.Abp.Core/Volo/Abp/Logging/InitLoggerFactory.cs
  9. 5
      framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModuleHelper.cs
  10. 13
      framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleLoader.cs
  11. 5
      framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceExtensions.cs
  12. 6
      framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceList.cs
  13. 4
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Modularity/ModuleLoader_Tests.cs

9
framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLoggingExtensions.cs

@ -1,12 +1,13 @@
using Volo.Abp.Logging; using Microsoft.Extensions.Logging;
using Volo.Abp.Logging;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
public static class ServiceCollectionLoggingExtensions public static class ServiceCollectionLoggingExtensions
{ {
public static IInitLogger GetInitLogger(this IServiceCollection services) public static ILogger<T> GetInitLogger<T>(this IServiceCollection services)
{ {
return services.GetSingletonInstance<IInitLogger>(); return services.GetSingletonInstance<IInitLoggerFactory>().Create<T>();
} }
} }
} }

18
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs

@ -62,7 +62,7 @@ namespace Volo.Abp
{ {
//TODO: Shutdown if not done before? //TODO: Shutdown if not done before?
} }
protected virtual void SetServiceProvider(IServiceProvider serviceProvider) protected virtual void SetServiceProvider(IServiceProvider serviceProvider)
{ {
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
@ -79,7 +79,7 @@ namespace Volo.Abp
.InitializeModules(new ApplicationInitializationContext(scope.ServiceProvider)); .InitializeModules(new ApplicationInitializationContext(scope.ServiceProvider));
} }
} }
protected virtual void WriteInitLogs(IServiceProvider serviceProvider) protected virtual void WriteInitLogs(IServiceProvider serviceProvider)
{ {
var logger = serviceProvider.GetService<ILogger<AbpApplicationBase>>(); var logger = serviceProvider.GetService<ILogger<AbpApplicationBase>>();
@ -87,14 +87,14 @@ namespace Volo.Abp
{ {
return; return;
} }
var initLogger = serviceProvider.GetRequiredService<IInitLogger>(); var initLogger = serviceProvider.GetRequiredService<IInitLoggerFactory>().Create<AbpApplicationBase>();
foreach (var entry in initLogger.Entries) foreach (var entry in initLogger.Entries)
{ {
logger.LogWithLevel(entry.Level, entry.Message, entry.Exception); logger.Log(entry.LogLevel, entry.EventId, entry.State, entry.Exception, entry.Formatter);
} }
initLogger.Entries.Clear(); initLogger.Entries.Clear();
} }
@ -108,7 +108,7 @@ namespace Volo.Abp
options.PlugInSources options.PlugInSources
); );
} }
//TODO: We can extract a new class for this //TODO: We can extract a new class for this
protected virtual void ConfigureServices() protected virtual void ConfigureServices()
{ {
@ -179,4 +179,4 @@ namespace Volo.Abp
} }
} }
} }
} }

6
framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs

@ -17,7 +17,7 @@ namespace Volo.Abp.Internal
} }
internal static void AddCoreAbpServices(this IServiceCollection services, internal static void AddCoreAbpServices(this IServiceCollection services,
IAbpApplication abpApplication, IAbpApplication abpApplication,
AbpApplicationCreationOptions applicationCreationOptions) AbpApplicationCreationOptions applicationCreationOptions)
{ {
var moduleLoader = new ModuleLoader(); var moduleLoader = new ModuleLoader();
@ -36,7 +36,7 @@ namespace Volo.Abp.Internal
services.TryAddSingleton<IModuleLoader>(moduleLoader); services.TryAddSingleton<IModuleLoader>(moduleLoader);
services.TryAddSingleton<IAssemblyFinder>(assemblyFinder); services.TryAddSingleton<IAssemblyFinder>(assemblyFinder);
services.TryAddSingleton<ITypeFinder>(typeFinder); services.TryAddSingleton<ITypeFinder>(typeFinder);
services.TryAddSingleton<IInitLogger>(new DefaultInitLogger()); services.TryAddSingleton<IInitLoggerFactory>(new InitLoggerFactory());
services.AddAssemblyOf<IAbpApplication>(); services.AddAssemblyOf<IAbpApplication>();
@ -49,4 +49,4 @@ namespace Volo.Abp.Internal
}); });
} }
} }
} }

30
framework/src/Volo.Abp.Core/Volo/Abp/Logging/AbpInitLogEntry.cs

@ -1,26 +1,20 @@
using System; using System;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Volo.Abp.Logging namespace Volo.Abp.Logging
{ {
public class AbpInitLogEntry public class AbpInitLogEntry
{ {
public LogLevel Level { get; } public LogLevel LogLevel { get; set; }
public string Message { get; } public EventId EventId { get; set; }
[CanBeNull] public object State { get; set; }
public Exception Exception { get; }
public Exception Exception { get; set; }
public AbpInitLogEntry(
LogLevel level, public Func<object, Exception, string> Formatter { get; set; }
string message,
Exception exception) public string Message => Formatter(State, Exception);
{
Level = level;
Message = message;
Exception = exception;
}
} }
} }

30
framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLogger.cs

@ -4,7 +4,7 @@ using Microsoft.Extensions.Logging;
namespace Volo.Abp.Logging namespace Volo.Abp.Logging
{ {
public class DefaultInitLogger : IInitLogger public class DefaultInitLogger<T> : IInitLogger<T>
{ {
public List<AbpInitLogEntry> Entries { get; } public List<AbpInitLogEntry> Entries { get; }
@ -12,13 +12,27 @@ namespace Volo.Abp.Logging
{ {
Entries = new List<AbpInitLogEntry>(); Entries = new List<AbpInitLogEntry>();
} }
public void Log( public virtual void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
LogLevel logLevel, {
string message, Entries.Add(new AbpInitLogEntry
Exception exception = null) {
LogLevel = logLevel,
EventId = eventId,
State = state,
Exception = exception,
Formatter = (s, e) => formatter((TState)s, e),
});
}
public virtual bool IsEnabled(LogLevel logLevel)
{
return logLevel != LogLevel.None;
}
public virtual IDisposable BeginScope<TState>(TState state)
{ {
Entries.Add(new AbpInitLogEntry(logLevel, message, exception)); return NullDisposable.Instance;
} }
} }
} }

12
framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLogger.cs

@ -1,16 +1,10 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Volo.Abp.Logging namespace Volo.Abp.Logging
{ {
public interface IInitLogger public interface IInitLogger<out T> : ILogger<T>
{ {
public List<AbpInitLogEntry> Entries { get; } public List<AbpInitLogEntry> Entries { get; }
void Log(
LogLevel logLevel,
string message,
Exception exception = null);
} }
} }

7
framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLoggerFactory.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.Logging
{
public interface IInitLoggerFactory
{
IInitLogger<T> Create<T>();
}
}

15
framework/src/Volo.Abp.Core/Volo/Abp/Logging/InitLoggerFactory.cs

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
namespace Volo.Abp.Logging
{
public class InitLoggerFactory : IInitLoggerFactory
{
private readonly Dictionary<Type, object> _cache = new Dictionary<Type, object>();
public virtual IInitLogger<T> Create<T>()
{
return (IInitLogger<T>)_cache.GetOrAdd(typeof(T), () => new DefaultInitLogger<T>());;
}
}
}

5
framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModuleHelper.cs

@ -3,13 +3,12 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Volo.Abp.Logging;
namespace Volo.Abp.Modularity namespace Volo.Abp.Modularity
{ {
internal static class AbpModuleHelper internal static class AbpModuleHelper
{ {
public static List<Type> FindAllModuleTypes(Type startupModuleType, IInitLogger logger) public static List<Type> FindAllModuleTypes(Type startupModuleType, ILogger logger)
{ {
var moduleTypes = new List<Type>(); var moduleTypes = new List<Type>();
logger.Log(LogLevel.Information, "Loaded ABP modules:"); logger.Log(LogLevel.Information, "Loaded ABP modules:");
@ -41,7 +40,7 @@ namespace Volo.Abp.Modularity
private static void AddModuleAndDependenciesResursively( private static void AddModuleAndDependenciesResursively(
List<Type> moduleTypes, List<Type> moduleTypes,
Type moduleType, Type moduleType,
IInitLogger logger, ILogger logger,
int depth = 0) int depth = 0)
{ {
AbpModule.CheckAbpModuleType(moduleType); AbpModule.CheckAbpModuleType(moduleType);

13
framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleLoader.cs

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Logging;
using Volo.Abp.Modularity.PlugIns; using Volo.Abp.Modularity.PlugIns;
namespace Volo.Abp.Modularity namespace Volo.Abp.Modularity
@ -26,7 +25,7 @@ namespace Volo.Abp.Modularity
} }
private List<IAbpModuleDescriptor> GetDescriptors( private List<IAbpModuleDescriptor> GetDescriptors(
IServiceCollection services, IServiceCollection services,
Type startupModuleType, Type startupModuleType,
PlugInSourceList plugInSources) PlugInSourceList plugInSources)
{ {
@ -44,16 +43,16 @@ namespace Volo.Abp.Modularity
Type startupModuleType, Type startupModuleType,
PlugInSourceList plugInSources) PlugInSourceList plugInSources)
{ {
var initLogger = services.GetInitLogger(); var logger = services.GetInitLogger<AbpApplicationBase>();
//All modules starting from the startup module //All modules starting from the startup module
foreach (var moduleType in AbpModuleHelper.FindAllModuleTypes(startupModuleType, initLogger)) foreach (var moduleType in AbpModuleHelper.FindAllModuleTypes(startupModuleType, logger))
{ {
modules.Add(CreateModuleDescriptor(services, moduleType)); modules.Add(CreateModuleDescriptor(services, moduleType));
} }
//Plugin modules //Plugin modules
foreach (var moduleType in plugInSources.GetAllModules(initLogger)) foreach (var moduleType in plugInSources.GetAllModules(logger))
{ {
if (modules.Any(m => m.Type == moduleType)) if (modules.Any(m => m.Type == moduleType))
{ {
@ -105,4 +104,4 @@ namespace Volo.Abp.Modularity
} }
} }
} }
} }

5
framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceExtensions.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Volo.Abp.Logging; using Volo.Abp.Logging;
namespace Volo.Abp.Modularity.PlugIns namespace Volo.Abp.Modularity.PlugIns
@ -8,7 +9,7 @@ namespace Volo.Abp.Modularity.PlugIns
public static class PlugInSourceExtensions public static class PlugInSourceExtensions
{ {
[NotNull] [NotNull]
public static Type[] GetModulesWithAllDependencies([NotNull] this IPlugInSource plugInSource, IInitLogger logger) public static Type[] GetModulesWithAllDependencies([NotNull] this IPlugInSource plugInSource, ILogger logger)
{ {
Check.NotNull(plugInSource, nameof(plugInSource)); Check.NotNull(plugInSource, nameof(plugInSource));
@ -19,4 +20,4 @@ namespace Volo.Abp.Modularity.PlugIns
.ToArray(); .ToArray();
} }
} }
} }

6
framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceList.cs

@ -2,14 +2,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp.Logging; using Microsoft.Extensions.Logging;
namespace Volo.Abp.Modularity.PlugIns namespace Volo.Abp.Modularity.PlugIns
{ {
public class PlugInSourceList : List<IPlugInSource> public class PlugInSourceList : List<IPlugInSource>
{ {
[NotNull] [NotNull]
internal Type[] GetAllModules(IInitLogger logger) internal Type[] GetAllModules(ILogger logger)
{ {
return this return this
.SelectMany(pluginSource => pluginSource.GetModulesWithAllDependencies(logger)) .SelectMany(pluginSource => pluginSource.GetModulesWithAllDependencies(logger))
@ -17,4 +17,4 @@ namespace Volo.Abp.Modularity.PlugIns
.ToArray(); .ToArray();
} }
} }
} }

4
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Modularity/ModuleLoader_Tests.cs

@ -14,7 +14,7 @@ namespace Volo.Abp.Modularity
var moduleLoader = new ModuleLoader(); var moduleLoader = new ModuleLoader();
var modules = moduleLoader.LoadModules( var modules = moduleLoader.LoadModules(
new ServiceCollection() new ServiceCollection()
.AddSingleton<IInitLogger>(new DefaultInitLogger()), .AddSingleton<IInitLoggerFactory>(new InitLoggerFactory()),
typeof(MyStartupModule), typeof(MyStartupModule),
new PlugInSourceList() new PlugInSourceList()
); );
@ -28,7 +28,7 @@ namespace Volo.Abp.Modularity
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
} }
} }
} }

Loading…
Cancel
Save