Skip to content

Refactor logging in generic classes #31364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Log = Microsoft.AspNetCore.Server.HttpSys.RequestContextLog;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!


namespace Microsoft.AspNetCore.Server.HttpSys
{
Expand Down Expand Up @@ -51,7 +52,7 @@ protected override async Task ExecuteAsync()
}
catch (Exception ex)
{
RequestContextLog.RequestProcessError(Logger, ex);
Log.RequestProcessError(Logger, ex);
if (context != null)
{
application.DisposeContext(context, ex);
Expand Down Expand Up @@ -85,14 +86,14 @@ protected override async Task ExecuteAsync()
{
if (messagePump.DecrementOutstandingRequest() == 0 && messagePump.Stopping)
{
RequestContextLog.RequestsDrained(Logger);
Log.RequestsDrained(Logger);
messagePump.SetShutdownSignal();
}
}
}
catch (Exception ex)
{
RequestContextLog.RequestError(Logger, ex);
Log.RequestError(Logger, ex);
Abort();
}
}
Expand Down
52 changes: 1 addition & 51 deletions src/SignalR/server/Core/src/HubConnectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Log = Microsoft.AspNetCore.SignalR.HubConnectionHandlerLog;

namespace Microsoft.AspNetCore.SignalR
{
Expand Down Expand Up @@ -335,56 +336,5 @@ private async Task DispatchMessagesAsync(HubConnectionContext connection)
}
}
}

private static class Log
{
private static readonly Action<ILogger, string, Exception?> _errorDispatchingHubEvent =
LoggerMessage.Define<string>(LogLevel.Error, new EventId(1, "ErrorDispatchingHubEvent"), "Error when dispatching '{HubMethod}' on hub.");

private static readonly Action<ILogger, Exception?> _errorProcessingRequest =
LoggerMessage.Define(LogLevel.Debug, new EventId(2, "ErrorProcessingRequest"), "Error when processing requests.");

private static readonly Action<ILogger, Exception?> _abortFailed =
LoggerMessage.Define(LogLevel.Trace, new EventId(3, "AbortFailed"), "Abort callback failed.");

private static readonly Action<ILogger, Exception?> _errorSendingClose =
LoggerMessage.Define(LogLevel.Debug, new EventId(4, "ErrorSendingClose"), "Error when sending Close message.");

private static readonly Action<ILogger, Exception?> _connectedStarting =
LoggerMessage.Define(LogLevel.Debug, new EventId(5, "ConnectedStarting"), "OnConnectedAsync started.");

private static readonly Action<ILogger, Exception?> _connectedEnding =
LoggerMessage.Define(LogLevel.Debug, new EventId(6, "ConnectedEnding"), "OnConnectedAsync ending.");

public static void ErrorDispatchingHubEvent(ILogger logger, string hubMethod, Exception exception)
{
_errorDispatchingHubEvent(logger, hubMethod, exception);
}

public static void ErrorProcessingRequest(ILogger logger, Exception exception)
{
_errorProcessingRequest(logger, exception);
}

public static void AbortFailed(ILogger logger, Exception exception)
{
_abortFailed(logger, exception);
}

public static void ErrorSendingClose(ILogger logger, Exception exception)
{
_errorSendingClose(logger, exception);
}

public static void ConnectedStarting(ILogger logger)
{
_connectedStarting(logger, null);
}

public static void ConnectedEnding(ILogger logger)
{
_connectedEnding(logger, null);
}
}
}
}
61 changes: 61 additions & 0 deletions src/SignalR/server/Core/src/HubConnectionHandlerLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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.Protocol;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.SignalR
{
internal static class HubConnectionHandlerLog
{
private static readonly Action<ILogger, string, Exception?> _errorDispatchingHubEvent =
LoggerMessage.Define<string>(LogLevel.Error, new EventId(1, "ErrorDispatchingHubEvent"), "Error when dispatching '{HubMethod}' on hub.");

private static readonly Action<ILogger, Exception?> _errorProcessingRequest =
LoggerMessage.Define(LogLevel.Debug, new EventId(2, "ErrorProcessingRequest"), "Error when processing requests.");

private static readonly Action<ILogger, Exception?> _abortFailed =
LoggerMessage.Define(LogLevel.Trace, new EventId(3, "AbortFailed"), "Abort callback failed.");

private static readonly Action<ILogger, Exception?> _errorSendingClose =
LoggerMessage.Define(LogLevel.Debug, new EventId(4, "ErrorSendingClose"), "Error when sending Close message.");

private static readonly Action<ILogger, Exception?> _connectedStarting =
LoggerMessage.Define(LogLevel.Debug, new EventId(5, "ConnectedStarting"), "OnConnectedAsync started.");

private static readonly Action<ILogger, Exception?> _connectedEnding =
LoggerMessage.Define(LogLevel.Debug, new EventId(6, "ConnectedEnding"), "OnConnectedAsync ending.");

public static void ErrorDispatchingHubEvent(ILogger logger, string hubMethod, Exception exception)
{
_errorDispatchingHubEvent(logger, hubMethod, exception);
}

public static void ErrorProcessingRequest(ILogger logger, Exception exception)
{
_errorProcessingRequest(logger, exception);
}

public static void AbortFailed(ILogger logger, Exception exception)
{
_abortFailed(logger, exception);
}

public static void ErrorSendingClose(ILogger logger, Exception exception)
{
_errorSendingClose(logger, exception);
}

public static void ConnectedStarting(ILogger logger)
{
_connectedStarting(logger, null);
}

public static void ConnectedEnding(ILogger logger)
{
_connectedEnding(logger, null);
}
}
}
199 changes: 0 additions & 199 deletions src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.Log.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Log = Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcherLog;

namespace Microsoft.AspNetCore.SignalR.Internal
{
Expand Down
Loading