diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs index fedc06fa1d..c6e3aa1d95 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs @@ -230,7 +230,7 @@ namespace Microsoft.AspNetCore.SignalR } catch (Exception ex) { - _logger.LogTrace(0, ex, "Abort callback failed"); + _logger.AbortFailed(ex); } using (var scope = _serviceScopeFactory.CreateScope()) diff --git a/src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRCoreLoggerExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRCoreLoggerExtensions.cs index 1dbdc07c48..faa055fb35 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRCoreLoggerExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRCoreLoggerExtensions.cs @@ -55,6 +55,9 @@ namespace Microsoft.AspNetCore.SignalR.Core.Internal private static readonly Action _unexpectedCancel = LoggerMessage.Define(LogLevel.Debug, new EventId(14, nameof(UnexpectedCancel)), "CancelInvocationMessage received unexpectedly."); + private static readonly Action _abortFailed = + LoggerMessage.Define(LogLevel.Trace, new EventId(15, nameof(AbortFailed)), "Abort callback failed."); + public static void UsingHubProtocol(this ILogger logger, string hubProtocol) { _usingHubProtocol(logger, hubProtocol, null); @@ -129,5 +132,10 @@ namespace Microsoft.AspNetCore.SignalR.Core.Internal { _unexpectedCancel(logger, null); } + + public static void AbortFailed(this ILogger logger, Exception exception) + { + _abortFailed(logger, exception); + } } } diff --git a/src/Microsoft.AspNetCore.SignalR.Redis/Internal/RedisLoggerExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Redis/Internal/RedisLoggerExtensions.cs new file mode 100644 index 0000000000..4d1871fdcb --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Redis/Internal/RedisLoggerExtensions.cs @@ -0,0 +1,60 @@ +// 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.Redis.Internal +{ + internal static class RedisLoggerExtensions + { + // Category: RedisHubLifetimeManager + private static readonly Action _connectingToEndpoints = + LoggerMessage.Define(LogLevel.Information, new EventId(0, nameof(ConnectingToEndpoints)), "Connecting to Redis endpoints: {endpoints}."); + + private static readonly Action _connected = + LoggerMessage.Define(LogLevel.Information, new EventId(1, nameof(Connected)), "Connected to Redis."); + + private static readonly Action _subscribing = + LoggerMessage.Define(LogLevel.Trace, new EventId(2, nameof(Subscribing)), "Subscribing to channel: {channel}."); + + private static readonly Action _receivedFromChannel = + LoggerMessage.Define(LogLevel.Trace, new EventId(3, nameof(ReceivedFromChannel)), "Received message from Redis channel {channel}."); + + private static readonly Action _publishToChannel = + LoggerMessage.Define(LogLevel.Trace, new EventId(4, nameof(PublishToChannel)), "Publishing message to Redis channel {channel}."); + + private static readonly Action _unsubscribe = + LoggerMessage.Define(LogLevel.Trace, new EventId(5, nameof(Unsubscribe)), "Unsubscribing from channel: {channel}."); + + public static void ConnectingToEndpoints(this ILogger logger, string endpoints) + { + _connectingToEndpoints(logger, endpoints, null); + } + + public static void Connected(this ILogger logger) + { + _connected(logger, null); + } + + public static void Subscribing(this ILogger logger, string channelName) + { + _subscribing(logger, channelName, null); + } + + public static void ReceivedFromChannel(this ILogger logger, string channelName) + { + _receivedFromChannel(logger, channelName, null); + } + + public static void PublishToChannel(this ILogger logger, string channelName) + { + _publishToChannel(logger, channelName, null); + } + + public static void Unsubscribe(this ILogger logger, string channelName) + { + _unsubscribe(logger, channelName, null); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs b/src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs index 9075e4764c..bfeed38163 100644 --- a/src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs +++ b/src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs @@ -51,11 +51,11 @@ namespace Microsoft.AspNetCore.SignalR.Redis _ackHandler = new AckHandler(); var writer = new LoggerTextWriter(logger); - _logger.LogInformation("Connecting to redis endpoints: {endpoints}", string.Join(", ", options.Value.Options.EndPoints.Select(e => EndPointCollection.ToString(e)))); + _logger.ConnectingToEndpoints(string.Join(", ", options.Value.Options.EndPoints.Select(e => EndPointCollection.ToString(e)))); _redisServerConnection = _options.Connect(writer); if (_redisServerConnection.IsConnected) { - _logger.LogInformation("Connected to redis"); + _logger.Connected(); } else { @@ -67,12 +67,12 @@ namespace Microsoft.AspNetCore.SignalR.Redis var previousBroadcastTask = Task.CompletedTask; var channelName = _channelNamePrefix; - _logger.LogInformation("Subscribing to channel: {channel}", channelName); + _logger.Subscribing(channelName); _bus.Subscribe(channelName, async (c, data) => { await previousBroadcastTask; - _logger.LogTrace("Received message from redis channel {channel}", channelName); + _logger.ReceivedFromChannel(channelName); var message = DeserializeMessage(data); @@ -89,12 +89,12 @@ namespace Microsoft.AspNetCore.SignalR.Redis var allExceptTask = Task.CompletedTask; channelName = _channelNamePrefix + ".AllExcept"; - _logger.LogInformation("Subscribing to channel: {channel}", channelName); + _logger.Subscribing(channelName); _bus.Subscribe(channelName, async (c, data) => { await allExceptTask; - _logger.LogTrace("Received message from redis channel {channel}", channelName); + _logger.ReceivedFromChannel(channelName); var message = DeserializeMessage(data); var excludedIds = message.ExcludedIds; @@ -222,7 +222,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis payload = stream.ToArray(); } - _logger.LogTrace("Publishing message to redis channel {channel}", channel); + _logger.PublishToChannel(channel); await _bus.PublishAsync(channel, payload); } @@ -242,7 +242,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis var previousConnectionTask = Task.CompletedTask; - _logger.LogInformation("Subscribing to connection channel: {channel}", connectionChannel); + _logger.Subscribing(connectionChannel); connectionTask = _bus.SubscribeAsync(connectionChannel, async (c, data) => { await previousConnectionTask; @@ -286,7 +286,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis { foreach (var subscription in redisSubscriptions) { - _logger.LogInformation("Unsubscribing from channel: {channel}", subscription); + _logger.Unsubscribe(subscription); tasks.Add(_bus.UnsubscribeAsync(subscription)); } } @@ -357,7 +357,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis var previousTask = Task.CompletedTask; - _logger.LogInformation("Subscribing to group channel: {channel}", groupChannel); + _logger.Subscribing(groupChannel); await _bus.SubscribeAsync(groupChannel, async (c, data) => { // Since this callback is async, we await the previous task then @@ -439,7 +439,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis if (group.Connections.Count == 0) { - _logger.LogInformation("Unsubscribing from group channel: {channel}", groupChannel); + _logger.Unsubscribe(groupChannel); await _bus.UnsubscribeAsync(groupChannel); } }