Skip to content

Commit e05640c

Browse files
Refactor logging in generic classes (#31364)
1 parent a3a0b0d commit e05640c

File tree

6 files changed

+263
-253
lines changed

6 files changed

+263
-253
lines changed

src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Tasks;
33
using Microsoft.AspNetCore.Hosting.Server;
44
using Microsoft.AspNetCore.Http;
5+
using Log = Microsoft.AspNetCore.Server.HttpSys.RequestContextLog;
56

67
namespace Microsoft.AspNetCore.Server.HttpSys
78
{
@@ -51,7 +52,7 @@ protected override async Task ExecuteAsync()
5152
}
5253
catch (Exception ex)
5354
{
54-
RequestContextLog.RequestProcessError(Logger, ex);
55+
Log.RequestProcessError(Logger, ex);
5556
if (context != null)
5657
{
5758
application.DisposeContext(context, ex);
@@ -85,14 +86,14 @@ protected override async Task ExecuteAsync()
8586
{
8687
if (messagePump.DecrementOutstandingRequest() == 0 && messagePump.Stopping)
8788
{
88-
RequestContextLog.RequestsDrained(Logger);
89+
Log.RequestsDrained(Logger);
8990
messagePump.SetShutdownSignal();
9091
}
9192
}
9293
}
9394
catch (Exception ex)
9495
{
95-
RequestContextLog.RequestError(Logger, ex);
96+
Log.RequestError(Logger, ex);
9697
Abort();
9798
}
9899
}

src/SignalR/server/Core/src/HubConnectionHandler.cs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.Extensions.DependencyInjection;
1515
using Microsoft.Extensions.Logging;
1616
using Microsoft.Extensions.Options;
17+
using Log = Microsoft.AspNetCore.SignalR.HubConnectionHandlerLog;
1718

1819
namespace Microsoft.AspNetCore.SignalR
1920
{
@@ -335,56 +336,5 @@ private async Task DispatchMessagesAsync(HubConnectionContext connection)
335336
}
336337
}
337338
}
338-
339-
private static class Log
340-
{
341-
private static readonly Action<ILogger, string, Exception?> _errorDispatchingHubEvent =
342-
LoggerMessage.Define<string>(LogLevel.Error, new EventId(1, "ErrorDispatchingHubEvent"), "Error when dispatching '{HubMethod}' on hub.");
343-
344-
private static readonly Action<ILogger, Exception?> _errorProcessingRequest =
345-
LoggerMessage.Define(LogLevel.Debug, new EventId(2, "ErrorProcessingRequest"), "Error when processing requests.");
346-
347-
private static readonly Action<ILogger, Exception?> _abortFailed =
348-
LoggerMessage.Define(LogLevel.Trace, new EventId(3, "AbortFailed"), "Abort callback failed.");
349-
350-
private static readonly Action<ILogger, Exception?> _errorSendingClose =
351-
LoggerMessage.Define(LogLevel.Debug, new EventId(4, "ErrorSendingClose"), "Error when sending Close message.");
352-
353-
private static readonly Action<ILogger, Exception?> _connectedStarting =
354-
LoggerMessage.Define(LogLevel.Debug, new EventId(5, "ConnectedStarting"), "OnConnectedAsync started.");
355-
356-
private static readonly Action<ILogger, Exception?> _connectedEnding =
357-
LoggerMessage.Define(LogLevel.Debug, new EventId(6, "ConnectedEnding"), "OnConnectedAsync ending.");
358-
359-
public static void ErrorDispatchingHubEvent(ILogger logger, string hubMethod, Exception exception)
360-
{
361-
_errorDispatchingHubEvent(logger, hubMethod, exception);
362-
}
363-
364-
public static void ErrorProcessingRequest(ILogger logger, Exception exception)
365-
{
366-
_errorProcessingRequest(logger, exception);
367-
}
368-
369-
public static void AbortFailed(ILogger logger, Exception exception)
370-
{
371-
_abortFailed(logger, exception);
372-
}
373-
374-
public static void ErrorSendingClose(ILogger logger, Exception exception)
375-
{
376-
_errorSendingClose(logger, exception);
377-
}
378-
379-
public static void ConnectedStarting(ILogger logger)
380-
{
381-
_connectedStarting(logger, null);
382-
}
383-
384-
public static void ConnectedEnding(ILogger logger)
385-
{
386-
_connectedEnding(logger, null);
387-
}
388-
}
389339
}
390340
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.SignalR.Protocol;
6+
using Microsoft.Extensions.Internal;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace Microsoft.AspNetCore.SignalR
10+
{
11+
internal static class HubConnectionHandlerLog
12+
{
13+
private static readonly Action<ILogger, string, Exception?> _errorDispatchingHubEvent =
14+
LoggerMessage.Define<string>(LogLevel.Error, new EventId(1, "ErrorDispatchingHubEvent"), "Error when dispatching '{HubMethod}' on hub.");
15+
16+
private static readonly Action<ILogger, Exception?> _errorProcessingRequest =
17+
LoggerMessage.Define(LogLevel.Debug, new EventId(2, "ErrorProcessingRequest"), "Error when processing requests.");
18+
19+
private static readonly Action<ILogger, Exception?> _abortFailed =
20+
LoggerMessage.Define(LogLevel.Trace, new EventId(3, "AbortFailed"), "Abort callback failed.");
21+
22+
private static readonly Action<ILogger, Exception?> _errorSendingClose =
23+
LoggerMessage.Define(LogLevel.Debug, new EventId(4, "ErrorSendingClose"), "Error when sending Close message.");
24+
25+
private static readonly Action<ILogger, Exception?> _connectedStarting =
26+
LoggerMessage.Define(LogLevel.Debug, new EventId(5, "ConnectedStarting"), "OnConnectedAsync started.");
27+
28+
private static readonly Action<ILogger, Exception?> _connectedEnding =
29+
LoggerMessage.Define(LogLevel.Debug, new EventId(6, "ConnectedEnding"), "OnConnectedAsync ending.");
30+
31+
public static void ErrorDispatchingHubEvent(ILogger logger, string hubMethod, Exception exception)
32+
{
33+
_errorDispatchingHubEvent(logger, hubMethod, exception);
34+
}
35+
36+
public static void ErrorProcessingRequest(ILogger logger, Exception exception)
37+
{
38+
_errorProcessingRequest(logger, exception);
39+
}
40+
41+
public static void AbortFailed(ILogger logger, Exception exception)
42+
{
43+
_abortFailed(logger, exception);
44+
}
45+
46+
public static void ErrorSendingClose(ILogger logger, Exception exception)
47+
{
48+
_errorSendingClose(logger, exception);
49+
}
50+
51+
public static void ConnectedStarting(ILogger logger)
52+
{
53+
_connectedStarting(logger, null);
54+
}
55+
56+
public static void ConnectedEnding(ILogger logger)
57+
{
58+
_connectedEnding(logger, null);
59+
}
60+
}
61+
}

src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.Log.cs

Lines changed: 0 additions & 199 deletions
This file was deleted.

src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Extensions.DependencyInjection;
1717
using Microsoft.Extensions.Internal;
1818
using Microsoft.Extensions.Logging;
19+
using Log = Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcherLog;
1920

2021
namespace Microsoft.AspNetCore.SignalR.Internal
2122
{

0 commit comments

Comments
 (0)