Add logging by default to HubConnectionBuilder (#1867)

- Use the AddLogging extension method by default in the HubConnection
- Removed WithConsoleLogger extension methods
- Removed WithLoggerFactory extension method (moved to test only)
- Added WithLogger that uses the new the new ILoggerBuilder
This commit is contained in:
David Fowler 2018-04-05 10:05:07 -07:00 committed by GitHub
parent 35b6d81f77
commit 11343ea15d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 37 additions and 58 deletions

View File

@ -32,7 +32,11 @@ namespace ClientSample
Console.WriteLine("Connecting to {0}", endpoint);
var connection = new HubConnectionBuilder()
.WithEndPoint(endpoint)
.WithConsoleLogger(LogLevel.Information)
.WithLogging(logging =>
{
logging.AddConsole();
logging.SetMinimumLevel(LogLevel.Trace);
})
.Build();
try

View File

@ -22,8 +22,8 @@ namespace Microsoft.AspNetCore.SignalR.Client
public HubConnectionBuilder()
{
Services = new ServiceCollection();
Services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
Services.AddSingleton<HubConnection>();
Services.AddLogging();
this.AddJsonProtocol();
}

View File

@ -24,22 +24,10 @@ namespace Microsoft.AspNetCore.SignalR.Client
return hubConnectionBuilder;
}
public static IHubConnectionBuilder WithLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory)
public static IHubConnectionBuilder WithLogging(this IHubConnectionBuilder hubConnectionBuilder, Action<ILoggingBuilder> configureLogging)
{
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
hubConnectionBuilder.Services.AddSingleton(loggerFactory);
hubConnectionBuilder.Services.AddLogging(configureLogging);
return hubConnectionBuilder;
}
public static IHubConnectionBuilder WithLogger(this IHubConnectionBuilder hubConnectionBuilder, Action<ILoggerFactory> configureLogging)
{
var loggerFactory = new LoggerFactory();
configureLogging(loggerFactory);
return hubConnectionBuilder.WithLoggerFactory(loggerFactory);
}
}
}

View File

@ -1,27 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.SignalR.Client
{
public static class HubConnectionBuilderExtensions
{
public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder)
{
return hubConnectionBuilder.WithLogger(loggerFactory =>
{
loggerFactory.AddConsole();
});
}
public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder, LogLevel logLevel)
{
return hubConnectionBuilder.WithLogger(loggerFactory =>
{
loggerFactory.AddConsole(logLevel);
});
}
}
}

View File

@ -57,18 +57,13 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}
[Fact]
public void WithConsoleLoggerAddsLogger()
public void DefaultLoggerFactoryExists()
{
var loggingFactory = Mock.Of<ILoggerFactory>();
var connectionBuilder = new HubConnectionBuilder();
connectionBuilder.WithLoggerFactory(loggingFactory);
var serviceProvider = connectionBuilder.Services.BuildServiceProvider();
var resolvedLoggingFactory = serviceProvider.GetService<ILoggerFactory>();
Assert.Same(resolvedLoggingFactory, loggingFactory);
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
Assert.NotNull(loggerFactory);
}
[Fact]

View File

@ -22,13 +22,6 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
Assert.Equal("Cannot create HubConnection instance. A connection was not configured.", ex.Message);
}
[Fact]
public void WithLoggerFactoryThrowsForNullLoggerFactory()
{
Assert.Equal("loggerFactory",
Assert.Throws<ArgumentNullException>(() => new HubConnectionBuilder().WithLoggerFactory(null)).ParamName);
}
[Fact]
public void AddJsonProtocolSetsHubProtocolToJsonWithDefaultOptions()
{

View File

@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.SignalR.Client
{
public static class HubConnectionBuilderTestExtensions
{
// Tests want to override the built in LoggerFactory, it internally calls AddLogging
// https://github.com/aspnet/Logging/blob/671af986ec3b46dc81e28e4a6c37a9d0ee283c65/src/Microsoft.Extensions.Logging.Testing/AssemblyTestLog.cs#L130
public static IHubConnectionBuilder WithLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory)
{
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
hubConnectionBuilder.Services.AddSingleton(loggerFactory);
return hubConnectionBuilder;
}
}
}

View File

@ -12,6 +12,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Common\Microsoft.AspNetCore.SignalR.Common.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Core\Microsoft.AspNetCore.SignalR.Core.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Client.Core\Microsoft.AspNetCore.SignalR.Client.Core.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Protocols.MsgPack\Microsoft.AspNetCore.SignalR.Protocols.MsgPack.csproj" />
</ItemGroup>