Skip to content

Commit

Permalink
Fix logger injection for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panyz522 committed May 8, 2019
1 parent a47e9cf commit 61b5ad6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
18 changes: 1 addition & 17 deletions SimCivil.Rpc/RpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,7 @@ private void CloseChannel(IChannel channel)
/// </summary>
/// <typeparam name="T">Category</typeparam>
/// <returns>Logger</returns>
protected ILogger<T> GetLogger<T>()
{
ILogger<T> logger;
if (logFac == null)
{
if (Container.IsRegistered<ILoggerFactory>())
{
logFac = Container.Resolve<ILoggerFactory>();
}
else
{
logFac = new LoggerFactory();
}
}
logger = logFac.CreateLogger<T>();
return logger;
}
protected ILogger<T> GetLogger<T>() => Container.Resolve<ILogger<T>>();

public event EventHandler<EventArgs<RpcRequest>> RemoteCalling;

Expand Down
13 changes: 12 additions & 1 deletion SimCivil.Test/RpcTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;


using Autofac;

using SimCivil.Rpc;

using Xunit;
using Xunit.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using Autofac.Extensions.DependencyInjection;

namespace SimCivil.Test
{
Expand All @@ -44,12 +48,19 @@ public class ServerFixture : IDisposable

public ServerFixture()
{
var services = new ServiceCollection();
services.AddLogging(lb => {
lb.AddConsole();
});

var builder = new ContainerBuilder();
builder.Populate(services);
builder.UseRpcSession();
builder.RegisterRpcProvider<TestServiceA, ITestServiceA>().InstancePerChannel();
builder.RegisterRpcProvider<TestServiceB, ITestServiceB>().SingleInstance();

Server = new RpcServer(builder.Build()) {Debug = true};
var container = builder.Build();
Server = new RpcServer(container) {Debug = true};
Server.Bind(9999);
Server.Run().Wait();
}
Expand Down
1 change: 1 addition & 0 deletions SimCivil.Test/SimCivil.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup>
<PackageReference Include="Autofac.Extras.FakeItEasy" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Microsoft.Orleans.OrleansRuntime" Version="2.2.0" />
Expand Down

0 comments on commit 61b5ad6

Please sign in to comment.