diff --git a/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLoggingExtensions.cs b/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLoggingExtensions.cs index 48921cb99b..e31e7cbf51 100644 --- a/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLoggingExtensions.cs +++ b/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 { public static class ServiceCollectionLoggingExtensions { - public static IInitLogger GetInitLogger(this IServiceCollection services) + public static ILogger GetInitLogger(this IServiceCollection services) { - return services.GetSingletonInstance(); + return services.GetSingletonInstance().Create(); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs index 256a1dd05d..61cee2ce90 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs @@ -62,7 +62,7 @@ namespace Volo.Abp { //TODO: Shutdown if not done before? } - + protected virtual void SetServiceProvider(IServiceProvider serviceProvider) { ServiceProvider = serviceProvider; @@ -79,7 +79,7 @@ namespace Volo.Abp .InitializeModules(new ApplicationInitializationContext(scope.ServiceProvider)); } } - + protected virtual void WriteInitLogs(IServiceProvider serviceProvider) { var logger = serviceProvider.GetService>(); @@ -87,14 +87,14 @@ namespace Volo.Abp { return; } - - var initLogger = serviceProvider.GetRequiredService(); - + + var initLogger = serviceProvider.GetRequiredService().Create(); + 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(); } @@ -108,7 +108,7 @@ namespace Volo.Abp options.PlugInSources ); } - + //TODO: We can extract a new class for this protected virtual void ConfigureServices() { @@ -179,4 +179,4 @@ namespace Volo.Abp } } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs index c116a20080..ee06003204 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs +++ b/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, - IAbpApplication abpApplication, + IAbpApplication abpApplication, AbpApplicationCreationOptions applicationCreationOptions) { var moduleLoader = new ModuleLoader(); @@ -36,7 +36,7 @@ namespace Volo.Abp.Internal services.TryAddSingleton(moduleLoader); services.TryAddSingleton(assemblyFinder); services.TryAddSingleton(typeFinder); - services.TryAddSingleton(new DefaultInitLogger()); + services.TryAddSingleton(new DefaultInitLoggerFactory()); services.AddAssemblyOf(); @@ -49,4 +49,4 @@ namespace Volo.Abp.Internal }); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/AbpInitLogEntry.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/AbpInitLogEntry.cs index 17a346fa1c..da44f2be2b 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/AbpInitLogEntry.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/AbpInitLogEntry.cs @@ -1,26 +1,20 @@ using System; -using JetBrains.Annotations; using Microsoft.Extensions.Logging; namespace Volo.Abp.Logging { public class AbpInitLogEntry { - public LogLevel Level { get; } - - public string Message { get; } - - [CanBeNull] - public Exception Exception { get; } - - public AbpInitLogEntry( - LogLevel level, - string message, - Exception exception) - { - Level = level; - Message = message; - Exception = exception; - } + public LogLevel LogLevel { get; set; } + + public EventId EventId { get; set; } + + public object State { get; set; } + + public Exception Exception { get; set; } + + public Func Formatter { get; set; } + + public string Message => Formatter(State, Exception); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLogger.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLogger.cs index e1f7344f39..2cc0963282 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLogger.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLogger.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.Logging; namespace Volo.Abp.Logging { - public class DefaultInitLogger : IInitLogger + public class DefaultInitLogger : IInitLogger { public List Entries { get; } @@ -12,13 +12,27 @@ namespace Volo.Abp.Logging { Entries = new List(); } - - public void Log( - LogLevel logLevel, - string message, - Exception exception = null) + + public virtual void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + Entries.Add(new AbpInitLogEntry + { + 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 state) { - Entries.Add(new AbpInitLogEntry(logLevel, message, exception)); + return NullDisposable.Instance; } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLoggerFactory.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLoggerFactory.cs new file mode 100644 index 0000000000..cb2e5d4060 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLoggerFactory.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Volo.Abp.Logging +{ + public class DefaultInitLoggerFactory : IInitLoggerFactory + { + private readonly Dictionary _cache = new Dictionary(); + + public virtual IInitLogger Create() + { + return (IInitLogger)_cache.GetOrAdd(typeof(T), () => new DefaultInitLogger());; + } + } +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLogger.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLogger.cs index 5c549682e9..bcb578394f 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLogger.cs +++ b/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; namespace Volo.Abp.Logging { - public interface IInitLogger + public interface IInitLogger : ILogger { public List Entries { get; } - - void Log( - LogLevel logLevel, - string message, - Exception exception = null); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLoggerFactory.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLoggerFactory.cs new file mode 100644 index 0000000000..91300f7967 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/IInitLoggerFactory.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.Logging +{ + public interface IInitLoggerFactory + { + IInitLogger Create(); + } +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModuleHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModuleHelper.cs index 9331014623..e8bccb8e18 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModuleHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModuleHelper.cs @@ -3,13 +3,12 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using Microsoft.Extensions.Logging; -using Volo.Abp.Logging; namespace Volo.Abp.Modularity { internal static class AbpModuleHelper { - public static List FindAllModuleTypes(Type startupModuleType, IInitLogger logger) + public static List FindAllModuleTypes(Type startupModuleType, ILogger logger) { var moduleTypes = new List(); logger.Log(LogLevel.Information, "Loaded ABP modules:"); @@ -41,7 +40,7 @@ namespace Volo.Abp.Modularity private static void AddModuleAndDependenciesResursively( List moduleTypes, Type moduleType, - IInitLogger logger, + ILogger logger, int depth = 0) { AbpModule.CheckAbpModuleType(moduleType); diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleLoader.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleLoader.cs index 56ee2f5d71..3c5fb9aa56 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleLoader.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleLoader.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Logging; using Volo.Abp.Modularity.PlugIns; namespace Volo.Abp.Modularity @@ -26,7 +25,7 @@ namespace Volo.Abp.Modularity } private List GetDescriptors( - IServiceCollection services, + IServiceCollection services, Type startupModuleType, PlugInSourceList plugInSources) { @@ -44,16 +43,16 @@ namespace Volo.Abp.Modularity Type startupModuleType, PlugInSourceList plugInSources) { - var initLogger = services.GetInitLogger(); - + var logger = services.GetInitLogger(); + //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)); } //Plugin modules - foreach (var moduleType in plugInSources.GetAllModules(initLogger)) + foreach (var moduleType in plugInSources.GetAllModules(logger)) { if (modules.Any(m => m.Type == moduleType)) { @@ -105,4 +104,4 @@ namespace Volo.Abp.Modularity } } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceExtensions.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceExtensions.cs index b041cd160d..92be156b69 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceExtensions.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using JetBrains.Annotations; +using Microsoft.Extensions.Logging; using Volo.Abp.Logging; namespace Volo.Abp.Modularity.PlugIns @@ -8,7 +9,7 @@ namespace Volo.Abp.Modularity.PlugIns public static class PlugInSourceExtensions { [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)); @@ -19,4 +20,4 @@ namespace Volo.Abp.Modularity.PlugIns .ToArray(); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceList.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceList.cs index 1d5509b359..06c0187a32 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceList.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/PlugIns/PlugInSourceList.cs @@ -2,14 +2,14 @@ using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; -using Volo.Abp.Logging; +using Microsoft.Extensions.Logging; namespace Volo.Abp.Modularity.PlugIns { public class PlugInSourceList : List { [NotNull] - internal Type[] GetAllModules(IInitLogger logger) + internal Type[] GetAllModules(ILogger logger) { return this .SelectMany(pluginSource => pluginSource.GetModulesWithAllDependencies(logger)) @@ -17,4 +17,4 @@ namespace Volo.Abp.Modularity.PlugIns .ToArray(); } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Modularity/ModuleLoader_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Modularity/ModuleLoader_Tests.cs index cc0117e184..4b7ed95e89 100644 --- a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Modularity/ModuleLoader_Tests.cs +++ b/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 modules = moduleLoader.LoadModules( new ServiceCollection() - .AddSingleton(new DefaultInitLogger()), + .AddSingleton(new DefaultInitLoggerFactory()), typeof(MyStartupModule), new PlugInSourceList() ); @@ -28,7 +28,7 @@ namespace Volo.Abp.Modularity { public override void ConfigureServices(ServiceConfigurationContext context) { - + } } }