diff --git a/src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs b/src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs index f5c01be..4ded32e 100644 --- a/src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs +++ b/src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs @@ -24,12 +24,12 @@ public static IApplicationBuilder UseRafty(this IApplicationBuilder builder, IMessageBus messageBus, IStateMachine stateMachine, IServiceRegistry serviceRegistry, - ILogger logger, + ILoggerFactory loggerFactory, IServersInCluster serversInCluster, string raftyBasePath = null) { builder.UseRaftyForTesting(baseUri, messageSender, messageBus, stateMachine, serviceRegistry, - logger, serversInCluster, raftyBasePath); + loggerFactory, serversInCluster, raftyBasePath); return builder; } @@ -40,13 +40,14 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve IMessageBus messageBus, IStateMachine stateMachine, IServiceRegistry serviceRegistry, - ILogger logger, + ILoggerFactory loggerFactory, IServersInCluster serversInCluster, string raftyBasePath = null) { var urlConfig = RaftyUrlConfig.Get(raftyBasePath); - var server = new Server(messageBus, serversInCluster, stateMachine, logger); + var server = new Server(messageBus, serversInCluster, stateMachine, loggerFactory); + var logger = loggerFactory.CreateLogger(); serviceRegistry.Register(new RegisterService(RaftyServiceDiscoveryName.Get(), server.Id, baseUri)); diff --git a/src/Rafty/Messaging/HttpClientMessageSender.cs b/src/Rafty/Messaging/HttpClientMessageSender.cs index 810b805..141914c 100644 --- a/src/Rafty/Messaging/HttpClientMessageSender.cs +++ b/src/Rafty/Messaging/HttpClientMessageSender.cs @@ -28,7 +28,7 @@ public class HttpClientMessageSender : IMessageSender, IDisposable private readonly string _commandUrl; private readonly ILogger _logger; - public HttpClientMessageSender(IServiceRegistry serviceRegistry, ILogger logger, string raftyBasePath = null) + public HttpClientMessageSender(IServiceRegistry serviceRegistry, ILoggerFactory loggerFactory, string raftyBasePath = null) { var urlConfig = RaftyUrlConfig.Get(raftyBasePath); @@ -36,7 +36,7 @@ public HttpClientMessageSender(IServiceRegistry serviceRegistry, ILogger logger, _requestVoteUrl = urlConfig.requestVoteUrl; _commandUrl = urlConfig.commandUrl; _serviceRegistry = serviceRegistry; - _logger = logger; + _logger = loggerFactory.CreateLogger(); _sendToSelfHandlers = new Dictionary> { {typeof(BecomeCandidate), x => _server.Receive((BecomeCandidate) x)}, diff --git a/src/Rafty/Raft/Server.cs b/src/Rafty/Raft/Server.cs index 9cfd424..8d84092 100644 --- a/src/Rafty/Raft/Server.cs +++ b/src/Rafty/Raft/Server.cs @@ -25,10 +25,10 @@ public class Server public Server(IMessageBus messageBus, IServersInCluster serversInCluster, IStateMachine stateMachine, - ILogger logger) + ILoggerFactory loggerFactory) { _stateMachine = stateMachine; - _logger = logger; + _logger = loggerFactory.CreateLogger(); _messageBus = messageBus; _serversInClusterInCluster = serversInCluster; Id = Guid.NewGuid(); diff --git a/test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs b/test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs index 55421fa..eff911a 100644 --- a/test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs +++ b/test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs @@ -290,7 +290,6 @@ private async Task GivenAServerIsRunning(string baseUrl) ServerInCluster serverInCluster = null; IMessageBus messageBus = null; IStateMachine stateMachine = null; - ILogger logger = null; var webHost = new WebHostBuilder() .UseUrls(baseUrl) @@ -298,19 +297,24 @@ private async Task GivenAServerIsRunning(string baseUrl) .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureServices(s => { - logger = new ConsoleLogger("ConsoleLogger", (x, y) => true, true); - s.AddSingleton(logger); - messageSender = new HttpClientMessageSender(_serviceRegistry, logger); - s.AddSingleton(messageSender); - messageBus = new InMemoryBus(messageSender); - s.AddSingleton(messageBus); - stateMachine = new FakeStateMachine(); - s.AddSingleton(stateMachine); + s.AddSingleton(); + s.AddSingleton(); + s.AddSingleton(); + s.AddSingleton(); + //s.AddSingleton(); }) .Configure(app => { + var loggerFactory = app.ApplicationServices.GetRequiredService(); + + messageSender = app.ApplicationServices.GetRequiredService(); + messageBus = app.ApplicationServices.GetRequiredService(); + stateMachine = app.ApplicationServices.GetRequiredService(); + //var _serviceRegistry = app.ApplicationServices.GetRequiredService(); + //_serversInCluster = app.ApplicationServices.GetRequiredService(); + var result = app.UseRaftyForTesting(new Uri(baseUrl), messageSender, messageBus, stateMachine, - _serviceRegistry, logger, _serversInCluster); + _serviceRegistry, loggerFactory, _serversInCluster); server = result.server; serverInCluster = result.serverInCluster; diff --git a/test/Rafty.UnitTests/AllServersTests.cs b/test/Rafty.UnitTests/AllServersTests.cs index 6eb3dd9..7b138d0 100644 --- a/test/Rafty.UnitTests/AllServersTests.cs +++ b/test/Rafty.UnitTests/AllServersTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Moq; using Rafty.Messages; @@ -186,7 +187,7 @@ private void ServerReceives(RequestVote requestVote) private void GivenANewServer() { _fakeStateMachine = new FakeStateMachine(); - _server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true)); + _server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new LoggerFactory()); } } } \ No newline at end of file diff --git a/test/Rafty.UnitTests/AppendEntriesTests.cs b/test/Rafty.UnitTests/AppendEntriesTests.cs index 8d7522f..c0b216e 100644 --- a/test/Rafty.UnitTests/AppendEntriesTests.cs +++ b/test/Rafty.UnitTests/AppendEntriesTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Rafty.AcceptanceTests; using Rafty.Messages; @@ -210,7 +211,7 @@ private void GivenANewServer() { _fakeStateMachine = new FakeStateMachine(); _messageBus = new FakeMessageBus(); - _server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true)); + _server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new LoggerFactory()); } private void ThenTheLogContainsEntriesCount(int expectedCount) diff --git a/test/Rafty.UnitTests/CandidateTests.cs b/test/Rafty.UnitTests/CandidateTests.cs index 9f03c83..38f05d7 100644 --- a/test/Rafty.UnitTests/CandidateTests.cs +++ b/test/Rafty.UnitTests/CandidateTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Moq; using Rafty.Messages; @@ -243,7 +244,7 @@ private void TheServerIsACandidate() private void GivenANewServer() { _fakeStateMachine = new FakeStateMachine(); - _server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true)); + _server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new LoggerFactory()); } } } diff --git a/test/Rafty.UnitTests/FollowerTests.cs b/test/Rafty.UnitTests/FollowerTests.cs index c547e7c..8865da8 100644 --- a/test/Rafty.UnitTests/FollowerTests.cs +++ b/test/Rafty.UnitTests/FollowerTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Moq; using Rafty.AcceptanceTests; @@ -75,13 +76,13 @@ private void GivenANewServer() { _fakeStateMachine = new FakeStateMachine(); _messageBus = new FakeMessageBus(); - _server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true)); + _server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new LoggerFactory()); } private void GivenANewServer(Mock mock) { _fakeStateMachine = new FakeStateMachine(); - _server = new Server(mock.Object, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true)); + _server = new Server(mock.Object, _serversInCluster, _fakeStateMachine, new LoggerFactory()); } private void TheServerIsAFollower() diff --git a/test/Rafty.UnitTests/InitialServerStateTests.cs b/test/Rafty.UnitTests/InitialServerStateTests.cs index abe738a..ddc87bf 100644 --- a/test/Rafty.UnitTests/InitialServerStateTests.cs +++ b/test/Rafty.UnitTests/InitialServerStateTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Rafty.Messaging; using Rafty.Raft; @@ -127,7 +128,7 @@ private void GivenANewServer() { _fakeStateMachine = new FakeStateMachine(); _messageBus = new FakeMessageBus(); - _server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true)); + _server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new LoggerFactory()); } private void ThenTheServerHasAnId() diff --git a/test/Rafty.UnitTests/LeaderTests.cs b/test/Rafty.UnitTests/LeaderTests.cs index 7dfa104..5fc2086 100644 --- a/test/Rafty.UnitTests/LeaderTests.cs +++ b/test/Rafty.UnitTests/LeaderTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Moq; using Moq.Language.Flow; @@ -421,7 +422,7 @@ private void TheServerIsALeader() private void GivenANewServer() { _fakeStateMachine = new FakeStateMachine(); - _server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true)); + _server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new LoggerFactory()); _serversInCluster.Add(new ServerInCluster(_server.Id)); } } diff --git a/test/Rafty.UnitTests/RequestVoteTests.cs b/test/Rafty.UnitTests/RequestVoteTests.cs index 9544fec..8e3b32a 100644 --- a/test/Rafty.UnitTests/RequestVoteTests.cs +++ b/test/Rafty.UnitTests/RequestVoteTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Rafty.AcceptanceTests; using Rafty.Messages; @@ -123,7 +124,7 @@ private void GivenANewServer() { _fakeStateMachine = new FakeStateMachine(); _messageBus = new FakeMessageBus(); - _server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true)); + _server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new LoggerFactory()); } private void GivenTheCurrentTermIs(int term)