From 2e695c935ec0fe8271f9ea618892c40c835160f9 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Wed, 13 Sep 2017 09:18:06 -0700 Subject: [PATCH] Made tweaks to .NET client logging API (#869) - Removed ConnectionFactoryDelegate and used Func - Changed WithLogger that accepts ILoggerFactory to WithLoggerFactory - Made UseLogger configure the existing ILoggerFactory or create a LoggerFactory - Add support for setting the log level for console logs - Updated tests --- samples/ClientSample/HubSample.cs | 30 ++++++++++++++----- .../ConnectionFactoryDelegate.cs | 9 ------ .../HubConnectionBuilder.cs | 5 ++-- .../HubConnectionBuilderExtensions.cs | 13 ++++++-- .../IHubConnectionBuilder.cs | 5 +++- ...soft.AspNetCore.SignalR.Client.Core.csproj | 2 +- .../HubConnectionBuilderExtensions.cs | 15 +++++----- ...Microsoft.AspNetCore.SignalR.Client.csproj | 1 - .../HubConnectionTests.cs | 2 +- .../HubConnectionBuilderExtensionsTests.cs | 5 ++-- .../HubConnectionBuilderTests.cs | 4 +-- .../EndToEndTests.cs | 2 +- 12 files changed, 57 insertions(+), 36 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.SignalR.Client.Core/ConnectionFactoryDelegate.cs diff --git a/samples/ClientSample/HubSample.cs b/samples/ClientSample/HubSample.cs index acdb5954ac..1dd478122e 100644 --- a/samples/ClientSample/HubSample.cs +++ b/samples/ClientSample/HubSample.cs @@ -31,16 +31,11 @@ namespace ClientSample baseUrl = string.IsNullOrEmpty(baseUrl) ? "http://localhost:5000/default" : baseUrl; Console.WriteLine("Connecting to {0}", baseUrl); - var httpConnection = new HttpConnection(new Uri(baseUrl)); - var connection = new HubConnectionBuilder() - .WithUrl(baseUrl) - .WithConsoleLogger() - .Build(); + HubConnection connection = await ConnectAsync(baseUrl); + Console.WriteLine("Connected to {0}", baseUrl); try { - await connection.StartAsync(); - Console.WriteLine("Connected to {0}", baseUrl); var cts = new CancellationTokenSource(); Console.CancelKeyPress += (sender, a) => @@ -92,5 +87,26 @@ namespace ClientSample } return 0; } + + private static async Task ConnectAsync(string baseUrl) + { + // Keep trying to until we can start + while (true) + { + var connection = new HubConnectionBuilder() + .WithUrl(baseUrl) + .WithConsoleLogger(LogLevel.Trace) + .Build(); + try + { + await connection.StartAsync(); + return connection; + } + catch (Exception) + { + await Task.Delay(1000); + } + } + } } } diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/ConnectionFactoryDelegate.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/ConnectionFactoryDelegate.cs deleted file mode 100644 index cdea9687de..0000000000 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/ConnectionFactoryDelegate.cs +++ /dev/null @@ -1,9 +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 Microsoft.AspNetCore.Sockets.Client; - -namespace Microsoft.AspNetCore.SignalR.Client -{ - public delegate IConnection ConnectionFactoryDelegate(); -} diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs index dbdb2c8e7b..2dca887414 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilder.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Microsoft.AspNetCore.Sockets.Client; using Newtonsoft.Json; namespace Microsoft.AspNetCore.SignalR.Client @@ -12,9 +13,9 @@ namespace Microsoft.AspNetCore.SignalR.Client public class HubConnectionBuilder : IHubConnectionBuilder { private readonly Dictionary, object> _settings = new Dictionary, object>(); - private ConnectionFactoryDelegate _connectionFactoryDelegate; + private Func _connectionFactoryDelegate; - public void ConfigureConnectionFactory(ConnectionFactoryDelegate connectionFactoryDelegate) => + public void ConfigureConnectionFactory(Func connectionFactoryDelegate) => _connectionFactoryDelegate = connectionFactoryDelegate; public void AddSetting(string name, T value) diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs index e24dccf369..a6b770d6cc 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnectionBuilderExtensions.cs @@ -1,6 +1,7 @@ // 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.AspNetCore.SignalR.Internal.Protocol; using Microsoft.Extensions.Logging; using Newtonsoft.Json; @@ -25,12 +26,20 @@ namespace Microsoft.AspNetCore.SignalR.Client return hubConnectionBuilder.WithHubProtocol(new MessagePackHubProtocol()); } - public static IHubConnectionBuilder WithLogger(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory) + public static IHubConnectionBuilder WithLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory) { - hubConnectionBuilder.AddSetting(HubConnectionBuilderDefaults.LoggerFactoryKey, loggerFactory); + hubConnectionBuilder.AddSetting(HubConnectionBuilderDefaults.LoggerFactoryKey, + loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory))); return hubConnectionBuilder; } + public static IHubConnectionBuilder WithLogger(this IHubConnectionBuilder hubConnectionBuilder, Action configureLogging) + { + var loggerFactory = hubConnectionBuilder.GetLoggerFactory() ?? new LoggerFactory(); + configureLogging(loggerFactory); + return hubConnectionBuilder.WithLoggerFactory(loggerFactory); + } + public static ILoggerFactory GetLoggerFactory(this IHubConnectionBuilder hubConnectionBuilder) { hubConnectionBuilder.TryGetSetting(HubConnectionBuilderDefaults.LoggerFactoryKey, out var loggerFactory); diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/IHubConnectionBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Client.Core/IHubConnectionBuilder.cs index 6d852398ca..f4463478fd 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/IHubConnectionBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/IHubConnectionBuilder.cs @@ -1,13 +1,16 @@ // 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.AspNetCore.Sockets.Client; + namespace Microsoft.AspNetCore.SignalR.Client { public interface IHubConnectionBuilder { void AddSetting(string name, T value); bool TryGetSetting(string name, out T value); - void ConfigureConnectionFactory(ConnectionFactoryDelegate connectionFactory); + void ConfigureConnectionFactory(Func connectionFactoryDelegate); HubConnection Build(); } } diff --git a/src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj b/src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj index eb6eb62e6a..ea44f93520 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj +++ b/src/Microsoft.AspNetCore.SignalR.Client.Core/Microsoft.AspNetCore.SignalR.Client.Core.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderExtensions.cs index b69d230c61..0ce5dfcdcf 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Client/HubConnectionBuilderExtensions.cs @@ -10,17 +10,18 @@ namespace Microsoft.AspNetCore.SignalR.Client { public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder) { - return hubConnectionBuilder.WithConsoleLogger(new LoggerFactory()); + return hubConnectionBuilder.WithLogger(loggerFactory => + { + loggerFactory.AddConsole(); + }); } - public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder, ILoggerFactory loggerFactory) + public static IHubConnectionBuilder WithConsoleLogger(this IHubConnectionBuilder hubConnectionBuilder, LogLevel logLevel) { - if (loggerFactory == null) + return hubConnectionBuilder.WithLogger(loggerFactory => { - throw new ArgumentNullException(nameof(loggerFactory)); - } - - return hubConnectionBuilder.WithLogger(loggerFactory.AddConsole()); + loggerFactory.AddConsole(logLevel); + }); } } } diff --git a/src/Microsoft.AspNetCore.SignalR.Client/Microsoft.AspNetCore.SignalR.Client.csproj b/src/Microsoft.AspNetCore.SignalR.Client/Microsoft.AspNetCore.SignalR.Client.csproj index c8a1985611..57e36021c6 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client/Microsoft.AspNetCore.SignalR.Client.csproj +++ b/src/Microsoft.AspNetCore.SignalR.Client/Microsoft.AspNetCore.SignalR.Client.csproj @@ -11,7 +11,6 @@ - diff --git a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs index 0b910f2405..799c059f03 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs @@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests var connection = new HubConnectionBuilder() .WithUrl(_serverFixture.BaseUrl + path) .WithTransportType(transportType) - .WithLogger(loggerFactory) + .WithLoggerFactory(loggerFactory) .WithHubProtocol(protocol) .Build(); diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs index a03b16bc8b..d93c28ebaa 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderExtensionsTests.cs @@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests { var connectionBuilder = new HubConnectionBuilder(); var loggerFactory = Mock.Of(); - connectionBuilder.WithLogger(loggerFactory); + connectionBuilder.WithLoggerFactory(loggerFactory); Assert.Same(loggerFactory, connectionBuilder.GetLoggerFactory()); } @@ -52,7 +52,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests { var connectionBuilder = new HubConnectionBuilder(); var mockLoggerFactory = new Mock(); - connectionBuilder.WithConsoleLogger(mockLoggerFactory.Object); + connectionBuilder.WithLoggerFactory(mockLoggerFactory.Object); + connectionBuilder.WithConsoleLogger(); mockLoggerFactory.Verify(f => f.AddProvider(It.IsAny()), Times.Once); } diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs index d3ad0e3b61..4223476913 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionBuilderTests.cs @@ -25,10 +25,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests } [Fact] - public void WithConsoleLoggerThrowsForNullLoggerFactory() + public void WithLoggerFactoryThrowsForNullLoggerFactory() { Assert.Equal("loggerFactory", - Assert.Throws(() => new HubConnectionBuilder().WithConsoleLogger(null)).ParamName); + Assert.Throws(() => new HubConnectionBuilder().WithLoggerFactory(null)).ParamName); } } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs index f2943c73ea..5006ada18c 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs @@ -287,7 +287,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests var connection = new HubConnectionBuilder() .WithUrl(new Uri(url)) .WithTransportType(transportType) - .WithLogger(loggerFactory) + .WithLoggerFactory(loggerFactory) .Build(); try {