Skip to content

Commit

Permalink
Update SignalR to use LoggerMessageAttribute (#40303)
Browse files Browse the repository at this point in the history
Contributes to #32087
  • Loading branch information
pranavkm authored Feb 22, 2022
1 parent 15b59f3 commit 2d6eb21
Show file tree
Hide file tree
Showing 24 changed files with 573 additions and 1,783 deletions.
719 changes: 177 additions & 542 deletions src/SignalR/clients/csharp/Client.Core/src/HubConnection.Log.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.AspNetCore.SignalR.Client.Internal;

internal abstract class InvocationRequest : IDisposable
internal abstract partial class InvocationRequest : IDisposable
{
private readonly CancellationTokenRegistration _cancellationTokenRegistration;

Expand Down Expand Up @@ -169,65 +169,33 @@ protected override void Cancel()
}
}

private static class Log
private static partial class Log
{
// Category: Streaming and NonStreaming
private static readonly Action<ILogger, string, Exception?> _invocationCreated =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(1, "InvocationCreated"), "Invocation {InvocationId} created.");

private static readonly Action<ILogger, string, Exception?> _invocationDisposed =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(2, "InvocationDisposed"), "Invocation {InvocationId} disposed.");
[LoggerMessage(1, LogLevel.Trace, "Invocation {InvocationId} created.", EventName = "InvocationCreated")]
public static partial void InvocationCreated(ILogger logger, string invocationId);

private static readonly Action<ILogger, string, Exception?> _invocationCompleted =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(3, "InvocationCompleted"), "Invocation {InvocationId} marked as completed.");
[LoggerMessage(2, LogLevel.Trace, "Invocation {InvocationId} disposed.", EventName = "InvocationDisposed")]
public static partial void InvocationDisposed(ILogger logger, string invocationId);

private static readonly Action<ILogger, string, Exception?> _invocationFailed =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(4, "InvocationFailed"), "Invocation {InvocationId} marked as failed.");
[LoggerMessage(3, LogLevel.Trace, "Invocation {InvocationId} marked as completed.", EventName = "InvocationCompleted")]
public static partial void InvocationCompleted(ILogger logger, string invocationId);

// Category: Streaming
private static readonly Action<ILogger, string, Exception> _errorWritingStreamItem =
LoggerMessage.Define<string>(LogLevel.Error, new EventId(5, "ErrorWritingStreamItem"), "Invocation {InvocationId} caused an error trying to write a stream item.");

private static readonly Action<ILogger, string, Exception?> _receivedUnexpectedComplete =
LoggerMessage.Define<string>(LogLevel.Error, new EventId(6, "ReceivedUnexpectedComplete"), "Invocation {InvocationId} received a completion result, but was invoked as a streaming invocation.");

// Category: NonStreaming
private static readonly Action<ILogger, string, Exception?> _streamItemOnNonStreamInvocation =
LoggerMessage.Define<string>(LogLevel.Error, new EventId(5, "StreamItemOnNonStreamInvocation"), "Invocation {InvocationId} received stream item but was invoked as a non-streamed invocation.");
[LoggerMessage(4, LogLevel.Trace, "Invocation {InvocationId} marked as failed.", EventName = "InvocationFailed")]
public static partial void InvocationFailed(ILogger logger, string invocationId);

public static void InvocationCreated(ILogger logger, string invocationId)
{
_invocationCreated(logger, invocationId, null);
}

public static void InvocationDisposed(ILogger logger, string invocationId)
{
_invocationDisposed(logger, invocationId, null);
}

public static void InvocationCompleted(ILogger logger, string invocationId)
{
_invocationCompleted(logger, invocationId, null);
}
// Category: Streaming

public static void InvocationFailed(ILogger logger, string invocationId)
{
_invocationFailed(logger, invocationId, null);
}
[LoggerMessage(5, LogLevel.Error, "Invocation {InvocationId} caused an error trying to write a stream item.", EventName = "ErrorWritingStreamItem")]
public static partial void ErrorWritingStreamItem(ILogger logger, string invocationId, Exception exception);

public static void ErrorWritingStreamItem(ILogger logger, string invocationId, Exception exception)
{
_errorWritingStreamItem(logger, invocationId, exception);
}
[LoggerMessage(6, LogLevel.Error, "Invocation {InvocationId} received a completion result, but was invoked as a streaming invocation.", EventName = "ReceivedUnexpectedComplete")]
public static partial void ReceivedUnexpectedComplete(ILogger logger, string invocationId);

public static void ReceivedUnexpectedComplete(ILogger logger, string invocationId)
{
_receivedUnexpectedComplete(logger, invocationId, null);
}
// Category: NonStreaming

public static void StreamItemOnNonStreamInvocation(ILogger logger, string invocationId)
{
_streamItemOnNonStreamInvocation(logger, invocationId, null);
}
[LoggerMessage(7, LogLevel.Error, "Invocation {InvocationId} received stream item but was invoked as a non-streamed invocation.", EventName = "StreamItemOnNonStreamInvocation")]
public static partial void StreamItemOnNonStreamInvocation(ILogger logger, string invocationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.AspNetCore.Http.Connections.Client.Internal;

internal class DefaultTransportFactory : ITransportFactory
internal sealed partial class DefaultTransportFactory : ITransportFactory
{
private readonly HttpClient? _httpClient;
private readonly HttpConnectionOptions _httpConnectionOptions;
Expand Down Expand Up @@ -61,14 +61,9 @@ public ITransport CreateTransport(HttpTransportType availableServerTransports)
throw new InvalidOperationException("No requested transports available on the server.");
}

private static class Log
private static partial class Log
{
private static readonly Action<ILogger, HttpTransportType, Exception> _transportNotSupported =
LoggerMessage.Define<HttpTransportType>(LogLevel.Debug, new EventId(1, "TransportNotSupported"), "Transport '{TransportType}' is not supported.");

public static void TransportNotSupported(ILogger logger, HttpTransportType transportType, Exception ex)
{
_transportNotSupported(logger, transportType, ex);
}
[LoggerMessage(1, LogLevel.Debug, "Transport '{TransportType}' is not supported.", EventName = "TransportNotSupported")]
public static partial void TransportNotSupported(ILogger logger, HttpTransportType transportType, Exception ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.AspNetCore.Http.Connections.Client.Internal;

internal class LoggingHttpMessageHandler : DelegatingHandler
internal sealed partial class LoggingHttpMessageHandler : DelegatingHandler
{
private readonly ILogger<LoggingHttpMessageHandler> _logger;

Expand Down Expand Up @@ -38,21 +38,12 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return response;
}

private static class Log
private static partial class Log
{
private static readonly Action<ILogger, HttpMethod, Uri, Exception?> _sendingHttpRequest =
LoggerMessage.Define<HttpMethod, Uri>(LogLevel.Trace, new EventId(1, "SendingHttpRequest"), "Sending HTTP request {RequestMethod} '{RequestUrl}'.");
[LoggerMessage(1, LogLevel.Trace, "Sending HTTP request {RequestMethod} '{RequestUrl}'.", EventName = "SendingHttpRequest")]
public static partial void SendingHttpRequest(ILogger logger, HttpMethod requestMethod, Uri requestUrl);

private static readonly Action<ILogger, int, HttpMethod, Uri, Exception?> _unsuccessfulHttpResponse =
LoggerMessage.Define<int, HttpMethod, Uri>(LogLevel.Warning, new EventId(2, "UnsuccessfulHttpResponse"), "Unsuccessful HTTP response {StatusCode} return from {RequestMethod} '{RequestUrl}'.");

public static void SendingHttpRequest(ILogger logger, HttpMethod requestMethod, Uri requestUrl)
{
_sendingHttpRequest(logger, requestMethod, requestUrl, null);
}
public static void UnsuccessfulHttpResponse(ILogger logger, HttpStatusCode statusCode, HttpMethod requestMethod, Uri requestUrl)
{
_unsuccessfulHttpResponse(logger, (int)statusCode, requestMethod, requestUrl, null);
}
[LoggerMessage(2, LogLevel.Warning, "Unsuccessful HTTP response {StatusCode} return from {RequestMethod} '{RequestUrl}'.", EventName = "UnsuccessfulHttpResponse")]
public static partial void UnsuccessfulHttpResponse(ILogger logger, HttpStatusCode statusCode, HttpMethod requestMethod, Uri requestUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,129 +10,59 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal;

internal partial class LongPollingTransport
{
private static class Log
// EventIds 100 - 106 used in SendUtils
private static partial class Log
{
private static readonly LogDefineOptions SkipEnabledCheckLogOptions = new() { SkipEnabledCheck = true };
[LoggerMessage(1, LogLevel.Information, "Starting transport. Transfer mode: {TransferFormat}.", EventName = "StartTransport")]
public static partial void StartTransport(ILogger logger, TransferFormat transferFormat);

private static readonly Action<ILogger, TransferFormat, Exception?> _startTransport =
LoggerMessage.Define<TransferFormat>(LogLevel.Information, new EventId(1, "StartTransport"), "Starting transport. Transfer mode: {TransferFormat}.");
[LoggerMessage(2, LogLevel.Debug, "Transport stopped.", EventName = "TransportStopped")]
public static partial void TransportStopped(ILogger logger, Exception? exception);

private static readonly Action<ILogger, Exception?> _transportStopped =
LoggerMessage.Define(LogLevel.Debug, new EventId(2, "TransportStopped"), "Transport stopped.");
[LoggerMessage(3, LogLevel.Debug, "Starting receive loop.", EventName = "StartReceive")]
public static partial void StartReceive(ILogger logger);

private static readonly Action<ILogger, Exception?> _startReceive =
LoggerMessage.Define(LogLevel.Debug, new EventId(3, "StartReceive"), "Starting receive loop.");
[LoggerMessage(6, LogLevel.Information, "Transport is stopping.", EventName = "TransportStopping")]
public static partial void TransportStopping(ILogger logger);

private static readonly Action<ILogger, Exception?> _receiveStopped =
LoggerMessage.Define(LogLevel.Debug, new EventId(4, "ReceiveStopped"), "Receive loop stopped.");
[LoggerMessage(5, LogLevel.Debug, "Receive loop canceled.", EventName = "ReceiveCanceled")]
public static partial void ReceiveCanceled(ILogger logger);

private static readonly Action<ILogger, Exception?> _receiveCanceled =
LoggerMessage.Define(LogLevel.Debug, new EventId(5, "ReceiveCanceled"), "Receive loop canceled.");
[LoggerMessage(4, LogLevel.Debug, "Receive loop stopped.", EventName = "ReceiveStopped")]
public static partial void ReceiveStopped(ILogger logger);

private static readonly Action<ILogger, Exception?> _transportStopping =
LoggerMessage.Define(LogLevel.Information, new EventId(6, "TransportStopping"), "Transport is stopping.");
[LoggerMessage(7, LogLevel.Debug, "The server is closing the connection.", EventName = "ClosingConnection")]
public static partial void ClosingConnection(ILogger logger);

private static readonly Action<ILogger, Exception?> _closingConnection =
LoggerMessage.Define(LogLevel.Debug, new EventId(7, "ClosingConnection"), "The server is closing the connection.");
[LoggerMessage(8, LogLevel.Debug, "Received messages from the server.", EventName = "ReceivedMessages")]
public static partial void ReceivedMessages(ILogger logger);

private static readonly Action<ILogger, Exception?> _receivedMessages =
LoggerMessage.Define(LogLevel.Debug, new EventId(8, "ReceivedMessages"), "Received messages from the server.");

private static readonly Action<ILogger, Uri, Exception> _errorPolling =
LoggerMessage.Define<Uri>(LogLevel.Error, new EventId(9, "ErrorPolling"), "Error while polling '{PollUrl}'.");
[LoggerMessage(9, LogLevel.Error, "Error while polling '{PollUrl}'.", EventName = "ErrorPolling")]
public static partial void ErrorPolling(ILogger logger, Uri pollUrl, Exception exception);

// long? does properly format as "(null)" when null.
private static readonly Action<ILogger, int, long?, Exception?> _pollResponseReceived =
LoggerMessage.Define<int, long?>(LogLevel.Trace, new EventId(10, "PollResponseReceived"),
"Poll response with status code {StatusCode} received from server. Content length: {ContentLength}.",
SkipEnabledCheckLogOptions);

private static readonly Action<ILogger, Uri, Exception?> _sendingDeleteRequest =
LoggerMessage.Define<Uri>(LogLevel.Debug, new EventId(11, "SendingDeleteRequest"), "Sending DELETE request to '{PollUrl}'.");

private static readonly Action<ILogger, Uri, Exception?> _deleteRequestAccepted =
LoggerMessage.Define<Uri>(LogLevel.Debug, new EventId(12, "DeleteRequestAccepted"), "DELETE request to '{PollUrl}' accepted.");

private static readonly Action<ILogger, Uri, Exception> _errorSendingDeleteRequest =
LoggerMessage.Define<Uri>(LogLevel.Error, new EventId(13, "ErrorSendingDeleteRequest"), "Error sending DELETE request to '{PollUrl}'.");

private static readonly Action<ILogger, Uri, Exception?> _connectionAlreadyClosedSendingDeleteRequest =
LoggerMessage.Define<Uri>(LogLevel.Debug, new EventId(14, "ConnectionAlreadyClosedSendingDeleteRequest"), "A 404 response was returned from sending DELETE request to '{PollUrl}', likely because the transport was already closed on the server.");

// EventIds 100 - 106 used in SendUtils

public static void StartTransport(ILogger logger, TransferFormat transferFormat)
{
_startTransport(logger, transferFormat, null);
}

public static void TransportStopped(ILogger logger, Exception? exception)
{
_transportStopped(logger, exception);
}

public static void StartReceive(ILogger logger)
{
_startReceive(logger, null);
}

public static void TransportStopping(ILogger logger)
{
_transportStopping(logger, null);
}

public static void ReceiveCanceled(ILogger logger)
{
_receiveCanceled(logger, null);
}

public static void ReceiveStopped(ILogger logger)
{
_receiveStopped(logger, null);
}

public static void ClosingConnection(ILogger logger)
{
_closingConnection(logger, null);
}

public static void ReceivedMessages(ILogger logger)
{
_receivedMessages(logger, null);
}

public static void ErrorPolling(ILogger logger, Uri pollUrl, Exception exception)
{
_errorPolling(logger, pollUrl, exception);
}

public static void PollResponseReceived(ILogger logger, HttpResponseMessage response)
{
if (logger.IsEnabled(LogLevel.Trace))
{
_pollResponseReceived(logger, (int)response.StatusCode,
response.Content.Headers.ContentLength ?? -1, null);
PollResponseReceived(logger, (int)response.StatusCode,
response.Content.Headers.ContentLength ?? -1);
}
}

public static void SendingDeleteRequest(ILogger logger, Uri pollUrl)
{
_sendingDeleteRequest(logger, pollUrl, null);
}
[LoggerMessage(10, LogLevel.Trace, "Poll response with status code {StatusCode} received from server. Content length: {ContentLength}.", EventName = "PollResponseReceived", SkipEnabledCheck = true)]
private static partial void PollResponseReceived(ILogger logger, int statusCode, long contentLength);

public static void DeleteRequestAccepted(ILogger logger, Uri pollUrl)
{
_deleteRequestAccepted(logger, pollUrl, null);
}
[LoggerMessage(11, LogLevel.Debug, "Sending DELETE request to '{PollUrl}'.", EventName = "SendingDeleteRequest")]
public static partial void SendingDeleteRequest(ILogger logger, Uri pollUrl);

public static void ErrorSendingDeleteRequest(ILogger logger, Uri pollUrl, Exception ex)
{
_errorSendingDeleteRequest(logger, pollUrl, ex);
}
[LoggerMessage(12, LogLevel.Debug, "DELETE request to '{PollUrl}' accepted.", EventName = "DeleteRequestAccepted")]
public static partial void DeleteRequestAccepted(ILogger logger, Uri pollUrl);

public static void ConnectionAlreadyClosedSendingDeleteRequest(ILogger logger, Uri pollUrl)
{
_connectionAlreadyClosedSendingDeleteRequest(logger, pollUrl, null);
}
[LoggerMessage(13, LogLevel.Error, "Error sending DELETE request to '{PollUrl}'.", EventName = "ErrorSendingDeleteRequest")]
public static partial void ErrorSendingDeleteRequest(ILogger logger, Uri pollUrl, Exception ex);

[LoggerMessage(14, LogLevel.Debug, "A 404 response was returned from sending DELETE request to '{PollUrl}', likely because the transport was already closed on the server.", EventName = "ConnectionAlreadyClosedSendingDeleteRequest")]
public static partial void ConnectionAlreadyClosedSendingDeleteRequest(ILogger logger, Uri pollUrl);
}
}
Loading

0 comments on commit 2d6eb21

Please sign in to comment.