diff --git a/samples/ClientSample/HubSample.cs b/samples/ClientSample/HubSample.cs index a480e65430..16f5005555 100644 --- a/samples/ClientSample/HubSample.cs +++ b/samples/ClientSample/HubSample.cs @@ -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 diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs index 5a17d67ca7..582e0544a6 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs @@ -22,8 +22,8 @@ namespace Microsoft.AspNetCore.SignalR.Client public HubConnectionBuilder() { Services = new ServiceCollection(); - Services.AddSingleton(NullLoggerFactory.Instance); Services.AddSingleton(); + Services.AddLogging(); this.AddJsonProtocol(); } diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs index 5cc0e7fb66..eea26471ba 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs @@ -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 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 configureLogging) - { - var loggerFactory = new LoggerFactory(); - configureLogging(loggerFactory); - return hubConnectionBuilder.WithLoggerFactory(loggerFactory); - } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderExtensions.cs deleted file mode 100644 index 0ce5dfcdcf..0000000000 --- a/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderExtensions.cs +++ /dev/null @@ -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); - }); - } - } -} diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs index fc966a0b39..85257f142b 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs @@ -57,18 +57,13 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests } [Fact] - public void WithConsoleLoggerAddsLogger() + public void DefaultLoggerFactoryExists() { - var loggingFactory = Mock.Of(); - var connectionBuilder = new HubConnectionBuilder(); - connectionBuilder.WithLoggerFactory(loggingFactory); - var serviceProvider = connectionBuilder.Services.BuildServiceProvider(); - var resolvedLoggingFactory = serviceProvider.GetService(); - - Assert.Same(resolvedLoggingFactory, loggingFactory); + var loggerFactory = serviceProvider.GetService(); + Assert.NotNull(loggerFactory); } [Fact] diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs index 958e1c7237..e13a122ec7 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs @@ -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(() => new HubConnectionBuilder().WithLoggerFactory(null)).ParamName); - } - [Fact] public void AddJsonProtocolSetsHubProtocolToJsonWithDefaultOptions() { diff --git a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/HubConnectionBuilderTestExtensions.cs b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/HubConnectionBuilderTestExtensions.cs new file mode 100644 index 0000000000..e9425a8540 --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/HubConnectionBuilderTestExtensions.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/Microsoft.AspNetCore.SignalR.Tests.Utils.csproj b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/Microsoft.AspNetCore.SignalR.Tests.Utils.csproj index af45cc59f3..5d99a4a543 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/Microsoft.AspNetCore.SignalR.Tests.Utils.csproj +++ b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/Microsoft.AspNetCore.SignalR.Tests.Utils.csproj @@ -12,6 +12,7 @@ +