Przeglądaj źródła

Address review comments

Lucas Trzesniewski 4 lat temu
rodzic
commit
37f9ee05bf

+ 1 - 1
src/Abc.Zebus.Directory.Runner/Abc.Zebus.Directory.Runner.csproj

@@ -12,11 +12,11 @@
     <ProjectReference Include="..\Abc.Zebus.Directory.Cassandra\Abc.Zebus.Directory.Cassandra.csproj" />
     <ProjectReference Include="..\Abc.Zebus.Directory.RocksDb\Abc.Zebus.Directory.RocksDb.csproj" />
     <ProjectReference Include="..\Abc.Zebus.Directory\Abc.Zebus.Directory.csproj" />
+    <ProjectReference Include="..\Abc.Zebus.Log4Net\Abc.Zebus.Log4Net.csproj" />
     <ProjectReference Include="..\Abc.Zebus\Abc.Zebus.csproj" />
   </ItemGroup>
 
   <ItemGroup>
-    <PackageReference Include="log4net" Version="2.0.12" />
     <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
   </ItemGroup>
 

+ 1 - 0
src/Abc.Zebus.Directory.Runner/Program.cs

@@ -11,6 +11,7 @@ using Abc.Zebus.Directory.Initialization;
 using Abc.Zebus.Directory.RocksDb.Storage;
 using Abc.Zebus.Directory.Storage;
 using Abc.Zebus.Dispatch;
+using Abc.Zebus.Log4Net;
 using Abc.Zebus.Util;
 using log4net;
 using log4net.Config;

+ 1 - 0
src/Abc.Zebus.Directory.Tests/Abc.Zebus.Directory.Tests.csproj

@@ -9,6 +9,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\Abc.Zebus.Directory\Abc.Zebus.Directory.csproj" />
+    <ProjectReference Include="..\Abc.Zebus.Log4Net\Abc.Zebus.Log4Net.csproj" />
     <ProjectReference Include="..\Abc.Zebus.Testing\Abc.Zebus.Testing.csproj" />
   </ItemGroup>
 

+ 19 - 0
src/Abc.Zebus.Directory.Tests/Log4netConfigurator.cs

@@ -0,0 +1,19 @@
+using System;
+using System.IO;
+using log4net.Config;
+using log4net.Core;
+using NUnit.Framework;
+
+namespace Abc.Zebus.Directory.Tests
+{
+    [SetUpFixture]
+    public class Log4netConfigurator
+    {
+        [OneTimeSetUp]
+        public void SetUp()
+        {
+            var configurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
+            XmlConfigurator.Configure(LoggerManager.GetRepository(typeof(Log4netConfigurator).Assembly), new FileInfo(configurationFile));
+        }
+    }
+}

+ 16 - 0
src/Abc.Zebus.Log4Net/Abc.Zebus.Log4Net.csproj

@@ -0,0 +1,16 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <TargetFramework>netstandard2.0</TargetFramework>
+    <Version>$(ZebusVersion)</Version>
+    <IsPackable>false</IsPackable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\Abc.Zebus\Abc.Zebus.csproj" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <PackageReference Include="log4net" Version="2.0.12" />
+  </ItemGroup>
+
+</Project>

+ 1 - 1
src/Abc.Zebus.Directory.Runner/Log4NetFactory.cs → src/Abc.Zebus.Log4Net/Log4NetFactory.cs

@@ -2,7 +2,7 @@ using System;
 using log4net;
 using Microsoft.Extensions.Logging;
 
-namespace Abc.Zebus.Directory.Runner
+namespace Abc.Zebus.Log4Net
 {
     public sealed class Log4NetFactory : ILoggerFactory
     {

+ 1 - 1
src/Abc.Zebus.Persistence.Runner/Abc.Zebus.Persistence.Runner.csproj

@@ -8,6 +8,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <ProjectReference Include="..\Abc.Zebus.Log4Net\Abc.Zebus.Log4Net.csproj" />
     <ProjectReference Include="..\Abc.Zebus.Persistence.CQL\Abc.Zebus.Persistence.CQL.csproj" />
     <ProjectReference Include="..\Abc.Zebus.Persistence.RocksDb\Abc.Zebus.Persistence.RocksDb.csproj" />
   </ItemGroup>
@@ -23,7 +24,6 @@
     <None Update="log4net.config">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
-    <Compile Include="..\Abc.Zebus.Directory.Runner\Log4NetFactory.cs" />
   </ItemGroup>
 
 </Project>

+ 1 - 1
src/Abc.Zebus.Persistence.Runner/Program.cs

@@ -4,8 +4,8 @@ using System.IO;
 using System.Threading;
 using Abc.Zebus.Core;
 using Abc.Zebus.Directory;
-using Abc.Zebus.Directory.Runner;
 using Abc.Zebus.Dispatch;
+using Abc.Zebus.Log4Net;
 using Abc.Zebus.Monitoring;
 using Abc.Zebus.Persistence.CQL;
 using Abc.Zebus.Persistence.CQL.PeriodicAction;

+ 1 - 0
src/Abc.Zebus.Tests/Abc.Zebus.Tests.csproj

@@ -8,6 +8,7 @@
   <Import Project="..\Abc.Zebus.Tests.props" />
 
   <ItemGroup>
+    <ProjectReference Include="..\Abc.Zebus.Log4Net\Abc.Zebus.Log4Net.csproj" />
     <ProjectReference Include="..\Abc.Zebus.Testing\Abc.Zebus.Testing.csproj" />
     <ProjectReference Include="..\Abc.Zebus\Abc.Zebus.csproj" />
   </ItemGroup>

+ 33 - 0
src/Abc.Zebus.Tests/Log4netConfigurator.cs

@@ -0,0 +1,33 @@
+using System;
+using System.IO;
+using JetBrains.Annotations;
+using log4net.Appender;
+using log4net.Config;
+using log4net.Core;
+using NUnit.Framework;
+
+namespace Abc.Zebus.Tests
+{
+    [SetUpFixture]
+    public class Log4netConfigurator
+    {
+        [OneTimeSetUp]
+        public void Setup()
+        {
+            var configurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
+            XmlConfigurator.Configure(LoggerManager.GetRepository(typeof(Log4netConfigurator).Assembly), new FileInfo(configurationFile));
+        }
+
+        [UsedImplicitly]
+        public class Appender : ConsoleAppender
+        {
+            protected override void Append(LoggingEvent loggingEvent)
+            {
+                lock (this)
+                {
+                    base.Append(loggingEvent);
+                }
+            }
+        }
+    }
+}

+ 6 - 0
src/Abc.Zebus.sln

@@ -57,6 +57,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Abc.Zebus.Directory.RocksDb
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Abc.Zebus.Directory.RocksDb.Tests", "Abc.Zebus.Directory.RocksDb.Tests\Abc.Zebus.Directory.RocksDb.Tests.csproj", "{3997C4BF-F051-4177-964D-B3465206A785}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Abc.Zebus.Log4Net", "Abc.Zebus.Log4Net\Abc.Zebus.Log4Net.csproj", "{73D5A20F-26EF-4633-BB2E-C21BBF3C89F5}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -143,6 +145,10 @@ Global
 		{3997C4BF-F051-4177-964D-B3465206A785}.Debug|Any CPU.Build.0 = Debug|x64
 		{3997C4BF-F051-4177-964D-B3465206A785}.Release|Any CPU.ActiveCfg = Release|x64
 		{3997C4BF-F051-4177-964D-B3465206A785}.Release|Any CPU.Build.0 = Release|x64
+		{73D5A20F-26EF-4633-BB2E-C21BBF3C89F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{73D5A20F-26EF-4633-BB2E-C21BBF3C89F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{73D5A20F-26EF-4633-BB2E-C21BBF3C89F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{73D5A20F-26EF-4633-BB2E-C21BBF3C89F5}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 1 - 1
src/Abc.Zebus/Core/BusMessageLogger.cs

@@ -10,7 +10,7 @@ using Microsoft.Extensions.Logging;
 
 namespace Abc.Zebus.Core
 {
-    public class BusMessageLogger
+    internal class BusMessageLogger
     {
         private static readonly ConcurrentDictionary<Type, MessageTypeLogHelper> _logHelpers = new ConcurrentDictionary<Type, MessageTypeLogHelper>();
         private readonly ILogger _logger;

+ 24 - 21
src/Abc.Zebus/ZebusLogManager.cs

@@ -27,43 +27,46 @@ namespace Abc.Zebus
         public static event Action? LoggerFactoryChanged;
 
         public static ILogger GetLogger(string name)
-            => new Logger(name);
+            => new ForwardingLogger(name);
 
         public static ILogger GetLogger(Type type)
             => GetLogger(type.FullName!);
 
-        private class Logger : ILogger
+        private class ForwardingLogger : ILogger
         {
             private readonly string _name;
-            private ILogger _logger = NullLogger.Instance;
+            private ILogger _logger;
             private ILoggerFactory _currentLoggerFactory = NullLoggerFactory.Instance;
 
-            public Logger(string name)
-                => _name = name;
+            private ILogger Logger
+            {
+                [MethodImpl(MethodImplOptions.AggressiveInlining)]
+                get => ReferenceEquals(_currentLoggerFactory, _loggerFactory)
+                    ? _logger
+                    : CreateLogger();
+            }
+
+            public ForwardingLogger(string name)
+            {
+                _name = name;
+                _logger = CreateLogger();
+            }
 
             public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
-                => GetLogger().Log(logLevel, eventId, state, exception, formatter);
+                => Logger.Log(logLevel, eventId, state, exception, formatter);
 
             public bool IsEnabled(LogLevel logLevel)
-                => GetLogger().IsEnabled(logLevel);
+                => Logger.IsEnabled(logLevel);
 
             public IDisposable BeginScope<TState>(TState state)
-                => GetLogger().BeginScope(state);
+                => Logger.BeginScope(state);
 
-            [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            private ILogger GetLogger()
+            [MethodImpl(MethodImplOptions.NoInlining)]
+            private ILogger CreateLogger()
             {
-                return ReferenceEquals(_currentLoggerFactory, _loggerFactory)
-                    ? _logger
-                    : GetLoggerSlow();
-
-                [MethodImpl(MethodImplOptions.NoInlining)]
-                ILogger GetLoggerSlow()
-                {
-                    _logger = _loggerFactory.CreateLogger(_name);
-                    _currentLoggerFactory = _loggerFactory;
-                    return _logger;
-                }
+                _logger = _loggerFactory.CreateLogger(_name);
+                _currentLoggerFactory = _loggerFactory;
+                return _logger;
             }
         }
     }