LoggerDefine for Redis (#1002)

This commit is contained in:
BrennanConroy 2017-10-18 10:46:03 -07:00 committed by GitHub
parent b3ac9f4bf8
commit bb308ff72e
4 changed files with 80 additions and 12 deletions

View File

@ -230,7 +230,7 @@ namespace Microsoft.AspNetCore.SignalR
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogTrace(0, ex, "Abort callback failed"); _logger.AbortFailed(ex);
} }
using (var scope = _serviceScopeFactory.CreateScope()) using (var scope = _serviceScopeFactory.CreateScope())

View File

@ -55,6 +55,9 @@ namespace Microsoft.AspNetCore.SignalR.Core.Internal
private static readonly Action<ILogger, Exception> _unexpectedCancel = private static readonly Action<ILogger, Exception> _unexpectedCancel =
LoggerMessage.Define(LogLevel.Debug, new EventId(14, nameof(UnexpectedCancel)), "CancelInvocationMessage received unexpectedly."); LoggerMessage.Define(LogLevel.Debug, new EventId(14, nameof(UnexpectedCancel)), "CancelInvocationMessage received unexpectedly.");
private static readonly Action<ILogger, Exception> _abortFailed =
LoggerMessage.Define(LogLevel.Trace, new EventId(15, nameof(AbortFailed)), "Abort callback failed.");
public static void UsingHubProtocol(this ILogger logger, string hubProtocol) public static void UsingHubProtocol(this ILogger logger, string hubProtocol)
{ {
_usingHubProtocol(logger, hubProtocol, null); _usingHubProtocol(logger, hubProtocol, null);
@ -129,5 +132,10 @@ namespace Microsoft.AspNetCore.SignalR.Core.Internal
{ {
_unexpectedCancel(logger, null); _unexpectedCancel(logger, null);
} }
public static void AbortFailed(this ILogger logger, Exception exception)
{
_abortFailed(logger, exception);
}
} }
} }

View File

@ -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<THub>
private static readonly Action<ILogger, string, Exception> _connectingToEndpoints =
LoggerMessage.Define<string>(LogLevel.Information, new EventId(0, nameof(ConnectingToEndpoints)), "Connecting to Redis endpoints: {endpoints}.");
private static readonly Action<ILogger, Exception> _connected =
LoggerMessage.Define(LogLevel.Information, new EventId(1, nameof(Connected)), "Connected to Redis.");
private static readonly Action<ILogger, string, Exception> _subscribing =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(2, nameof(Subscribing)), "Subscribing to channel: {channel}.");
private static readonly Action<ILogger, string, Exception> _receivedFromChannel =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(3, nameof(ReceivedFromChannel)), "Received message from Redis channel {channel}.");
private static readonly Action<ILogger, string, Exception> _publishToChannel =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(4, nameof(PublishToChannel)), "Publishing message to Redis channel {channel}.");
private static readonly Action<ILogger, string, Exception> _unsubscribe =
LoggerMessage.Define<string>(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);
}
}
}

View File

@ -51,11 +51,11 @@ namespace Microsoft.AspNetCore.SignalR.Redis
_ackHandler = new AckHandler(); _ackHandler = new AckHandler();
var writer = new LoggerTextWriter(logger); 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); _redisServerConnection = _options.Connect(writer);
if (_redisServerConnection.IsConnected) if (_redisServerConnection.IsConnected)
{ {
_logger.LogInformation("Connected to redis"); _logger.Connected();
} }
else else
{ {
@ -67,12 +67,12 @@ namespace Microsoft.AspNetCore.SignalR.Redis
var previousBroadcastTask = Task.CompletedTask; var previousBroadcastTask = Task.CompletedTask;
var channelName = _channelNamePrefix; var channelName = _channelNamePrefix;
_logger.LogInformation("Subscribing to channel: {channel}", channelName); _logger.Subscribing(channelName);
_bus.Subscribe(channelName, async (c, data) => _bus.Subscribe(channelName, async (c, data) =>
{ {
await previousBroadcastTask; await previousBroadcastTask;
_logger.LogTrace("Received message from redis channel {channel}", channelName); _logger.ReceivedFromChannel(channelName);
var message = DeserializeMessage<HubMessage>(data); var message = DeserializeMessage<HubMessage>(data);
@ -89,12 +89,12 @@ namespace Microsoft.AspNetCore.SignalR.Redis
var allExceptTask = Task.CompletedTask; var allExceptTask = Task.CompletedTask;
channelName = _channelNamePrefix + ".AllExcept"; channelName = _channelNamePrefix + ".AllExcept";
_logger.LogInformation("Subscribing to channel: {channel}", channelName); _logger.Subscribing(channelName);
_bus.Subscribe(channelName, async (c, data) => _bus.Subscribe(channelName, async (c, data) =>
{ {
await allExceptTask; await allExceptTask;
_logger.LogTrace("Received message from redis channel {channel}", channelName); _logger.ReceivedFromChannel(channelName);
var message = DeserializeMessage<RedisExcludeClientsMessage>(data); var message = DeserializeMessage<RedisExcludeClientsMessage>(data);
var excludedIds = message.ExcludedIds; var excludedIds = message.ExcludedIds;
@ -222,7 +222,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis
payload = stream.ToArray(); payload = stream.ToArray();
} }
_logger.LogTrace("Publishing message to redis channel {channel}", channel); _logger.PublishToChannel(channel);
await _bus.PublishAsync(channel, payload); await _bus.PublishAsync(channel, payload);
} }
@ -242,7 +242,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis
var previousConnectionTask = Task.CompletedTask; var previousConnectionTask = Task.CompletedTask;
_logger.LogInformation("Subscribing to connection channel: {channel}", connectionChannel); _logger.Subscribing(connectionChannel);
connectionTask = _bus.SubscribeAsync(connectionChannel, async (c, data) => connectionTask = _bus.SubscribeAsync(connectionChannel, async (c, data) =>
{ {
await previousConnectionTask; await previousConnectionTask;
@ -286,7 +286,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis
{ {
foreach (var subscription in redisSubscriptions) foreach (var subscription in redisSubscriptions)
{ {
_logger.LogInformation("Unsubscribing from channel: {channel}", subscription); _logger.Unsubscribe(subscription);
tasks.Add(_bus.UnsubscribeAsync(subscription)); tasks.Add(_bus.UnsubscribeAsync(subscription));
} }
} }
@ -357,7 +357,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis
var previousTask = Task.CompletedTask; var previousTask = Task.CompletedTask;
_logger.LogInformation("Subscribing to group channel: {channel}", groupChannel); _logger.Subscribing(groupChannel);
await _bus.SubscribeAsync(groupChannel, async (c, data) => await _bus.SubscribeAsync(groupChannel, async (c, data) =>
{ {
// Since this callback is async, we await the previous task then // 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) if (group.Connections.Count == 0)
{ {
_logger.LogInformation("Unsubscribing from group channel: {channel}", groupChannel); _logger.Unsubscribe(groupChannel);
await _bus.UnsubscribeAsync(groupChannel); await _bus.UnsubscribeAsync(groupChannel);
} }
} }