// 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 System.Linq; using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.SignalR.Client { public partial class HubConnection { private static class Log { private static readonly Action _preparingNonBlockingInvocation = LoggerMessage.Define(LogLevel.Trace, new EventId(1, "PreparingNonBlockingInvocation"), "Preparing non-blocking invocation of '{Target}', with {ArgumentCount} argument(s)."); private static readonly Action _preparingBlockingInvocation = LoggerMessage.Define(LogLevel.Trace, new EventId(2, "PreparingBlockingInvocation"), "Preparing blocking invocation '{InvocationId}' of '{Target}', with return type '{ReturnType}' and {ArgumentCount} argument(s)."); private static readonly Action _registeringInvocation = LoggerMessage.Define(LogLevel.Debug, new EventId(3, "RegisteringInvocation"), "Registering Invocation ID '{InvocationId}' for tracking."); private static readonly Action _issuingInvocation = LoggerMessage.Define(LogLevel.Trace, new EventId(4, "IssuingInvocation"), "Issuing Invocation '{InvocationId}': {ReturnType} {MethodName}({Args})."); private static readonly Action _sendingMessage = LoggerMessage.Define(LogLevel.Debug, new EventId(5, "SendingMessage"), "Sending {MessageType} message '{InvocationId}'."); private static readonly Action _messageSent = LoggerMessage.Define(LogLevel.Debug, new EventId(6, "MessageSent"), "Sending {MessageType} message '{InvocationId}' completed."); private static readonly Action _failedToSendInvocation = LoggerMessage.Define(LogLevel.Error, new EventId(7, "FailedToSendInvocation"), "Sending Invocation '{InvocationId}' failed."); private static readonly Action _receivedInvocation = LoggerMessage.Define(LogLevel.Trace, new EventId(8, "ReceivedInvocation"), "Received Invocation '{InvocationId}': {MethodName}({Args})."); private static readonly Action _droppedCompletionMessage = LoggerMessage.Define(LogLevel.Warning, new EventId(9, "DroppedCompletionMessage"), "Dropped unsolicited Completion message for invocation '{InvocationId}'."); private static readonly Action _droppedStreamMessage = LoggerMessage.Define(LogLevel.Warning, new EventId(10, "DroppedStreamMessage"), "Dropped unsolicited StreamItem message for invocation '{InvocationId}'."); private static readonly Action _shutdownConnection = LoggerMessage.Define(LogLevel.Trace, new EventId(11, "ShutdownConnection"), "Shutting down connection."); private static readonly Action _shutdownWithError = LoggerMessage.Define(LogLevel.Error, new EventId(12, "ShutdownWithError"), "Connection is shutting down due to an error."); private static readonly Action _removingInvocation = LoggerMessage.Define(LogLevel.Trace, new EventId(13, "RemovingInvocation"), "Removing pending invocation {InvocationId}."); private static readonly Action _missingHandler = LoggerMessage.Define(LogLevel.Warning, new EventId(14, "MissingHandler"), "Failed to find handler for '{Target}' method."); private static readonly Action _receivedStreamItem = LoggerMessage.Define(LogLevel.Trace, new EventId(15, "ReceivedStreamItem"), "Received StreamItem for Invocation {InvocationId}."); private static readonly Action _cancelingStreamItem = LoggerMessage.Define(LogLevel.Trace, new EventId(16, "CancelingStreamItem"), "Canceling dispatch of StreamItem message for Invocation {InvocationId}. The invocation was canceled."); private static readonly Action _receivedStreamItemAfterClose = LoggerMessage.Define(LogLevel.Warning, new EventId(17, "ReceivedStreamItemAfterClose"), "Invocation {InvocationId} received stream item after channel was closed."); private static readonly Action _receivedInvocationCompletion = LoggerMessage.Define(LogLevel.Trace, new EventId(18, "ReceivedInvocationCompletion"), "Received Completion for Invocation {InvocationId}."); private static readonly Action _cancelingInvocationCompletion = LoggerMessage.Define(LogLevel.Trace, new EventId(19, "CancelingInvocationCompletion"), "Canceling dispatch of Completion message for Invocation {InvocationId}. The invocation was canceled."); private static readonly Action _releasingConnectionLock = LoggerMessage.Define(LogLevel.Trace, new EventId(20, "ReleasingConnectionLock"), "Releasing Connection Lock in {MethodName} ({FilePath}:{LineNumber})."); private static readonly Action _stopped = LoggerMessage.Define(LogLevel.Debug, new EventId(21, "Stopped"), "HubConnection stopped."); private static readonly Action _invocationAlreadyInUse = LoggerMessage.Define(LogLevel.Critical, new EventId(22, "InvocationAlreadyInUse"), "Invocation ID '{InvocationId}' is already in use."); private static readonly Action _receivedUnexpectedResponse = LoggerMessage.Define(LogLevel.Error, new EventId(23, "ReceivedUnexpectedResponse"), "Unsolicited response received for invocation '{InvocationId}'."); private static readonly Action _hubProtocol = LoggerMessage.Define(LogLevel.Information, new EventId(24, "HubProtocol"), "Using HubProtocol '{Protocol} v{Version}'."); private static readonly Action _preparingStreamingInvocation = LoggerMessage.Define(LogLevel.Trace, new EventId(25, "PreparingStreamingInvocation"), "Preparing streaming invocation '{InvocationId}' of '{Target}', with return type '{ReturnType}' and {ArgumentCount} argument(s)."); private static readonly Action _resettingKeepAliveTimer = LoggerMessage.Define(LogLevel.Trace, new EventId(26, "ResettingKeepAliveTimer"), "Resetting keep-alive timer, received a message from the server."); private static readonly Action _errorDuringClosedEvent = LoggerMessage.Define(LogLevel.Error, new EventId(27, "ErrorDuringClosedEvent"), "An exception was thrown in the handler for the Closed event."); private static readonly Action _sendingHubHandshake = LoggerMessage.Define(LogLevel.Debug, new EventId(28, "SendingHubHandshake"), "Sending Hub Handshake."); private static readonly Action _receivedPing = LoggerMessage.Define(LogLevel.Trace, new EventId(31, "ReceivedPing"), "Received a ping message."); private static readonly Action _errorInvokingClientSideMethod = LoggerMessage.Define(LogLevel.Error, new EventId(34, "ErrorInvokingClientSideMethod"), "Invoking client side method '{MethodName}' failed."); private static readonly Action _errorProcessingHandshakeResponse = LoggerMessage.Define(LogLevel.Error, new EventId(35, "ErrorReceivingHandshakeResponse"), "Error processing the handshake response."); private static readonly Action _handshakeServerError = LoggerMessage.Define(LogLevel.Error, new EventId(36, "HandshakeServerError"), "Server returned handshake error: {Error}"); private static readonly Action _receivedClose = LoggerMessage.Define(LogLevel.Debug, new EventId(37, "ReceivedClose"), "Received close message."); private static readonly Action _receivedCloseWithError = LoggerMessage.Define(LogLevel.Error, new EventId(38, "ReceivedCloseWithError"), "Received close message with an error: {Error}"); private static readonly Action _handshakeComplete = LoggerMessage.Define(LogLevel.Debug, new EventId(39, "HandshakeComplete"), "Handshake with server complete."); private static readonly Action _registeringHandler = LoggerMessage.Define(LogLevel.Debug, new EventId(40, "RegisteringHandler"), "Registering handler for client method '{MethodName}'."); private static readonly Action _starting = LoggerMessage.Define(LogLevel.Debug, new EventId(41, "Starting"), "Starting HubConnection."); private static readonly Action _waitingOnConnectionLock = LoggerMessage.Define(LogLevel.Trace, new EventId(42, "WaitingOnConnectionLock"), "Waiting on Connection Lock in {MethodName} ({FilePath}:{LineNumber})."); private static readonly Action _errorStartingConnection = LoggerMessage.Define(LogLevel.Error, new EventId(43, "ErrorStartingConnection"), "Error starting connection."); private static readonly Action _started = LoggerMessage.Define(LogLevel.Information, new EventId(44, "Started"), "HubConnection started."); private static readonly Action _sendingCancellation = LoggerMessage.Define(LogLevel.Debug, new EventId(45, "SendingCancellation"), "Sending Cancellation for Invocation '{InvocationId}'."); private static readonly Action _cancelingOutstandingInvocations = LoggerMessage.Define(LogLevel.Debug, new EventId(46, "CancelingOutstandingInvocations"), "Canceling all outstanding invocations."); private static readonly Action _receiveLoopStarting = LoggerMessage.Define(LogLevel.Debug, new EventId(47, "ReceiveLoopStarting"), "Receive loop starting."); private static readonly Action _startingServerTimeoutTimer = LoggerMessage.Define(LogLevel.Debug, new EventId(48, "StartingServerTimeoutTimer"), "Starting server timeout timer. Duration: {ServerTimeout:0.00}ms"); private static readonly Action _notUsingServerTimeout = LoggerMessage.Define(LogLevel.Debug, new EventId(49, "NotUsingServerTimeout"), "Not using server timeout because the transport inherently tracks server availability."); private static readonly Action _serverDisconnectedWithError = LoggerMessage.Define(LogLevel.Error, new EventId(50, "ServerDisconnectedWithError"), "The server connection was terminated with an error."); private static readonly Action _invokingClosedEventHandler = LoggerMessage.Define(LogLevel.Debug, new EventId(51, "InvokingClosedEventHandler"), "Invoking the Closed event handler."); private static readonly Action _stopping = LoggerMessage.Define(LogLevel.Debug, new EventId(52, "Stopping"), "Stopping HubConnection."); private static readonly Action _terminatingReceiveLoop = LoggerMessage.Define(LogLevel.Debug, new EventId(53, "TerminatingReceiveLoop"), "Terminating receive loop."); private static readonly Action _waitingForReceiveLoopToTerminate = LoggerMessage.Define(LogLevel.Debug, new EventId(54, "WaitingForReceiveLoopToTerminate"), "Waiting for the receive loop to terminate."); private static readonly Action _unableToSendCancellation = LoggerMessage.Define(LogLevel.Trace, new EventId(55, "UnableToSendCancellation"), "Unable to send cancellation for invocation '{InvocationId}'. The connection is inactive."); private static readonly Action _processingMessage = LoggerMessage.Define(LogLevel.Debug, new EventId(56, "ProcessingMessage"), "Processing {MessageLength} byte message from server."); public static void PreparingNonBlockingInvocation(ILogger logger, string target, int count) { _preparingNonBlockingInvocation(logger, target, count, null); } public static void PreparingBlockingInvocation(ILogger logger, string invocationId, string target, string returnType, int count) { _preparingBlockingInvocation(logger, invocationId, target, returnType, count, null); } public static void PreparingStreamingInvocation(ILogger logger, string invocationId, string target, string returnType, int count) { _preparingStreamingInvocation(logger, invocationId, target, returnType, count, null); } public static void RegisteringInvocation(ILogger logger, string invocationId) { _registeringInvocation(logger, invocationId, null); } public static void IssuingInvocation(ILogger logger, string invocationId, string returnType, string methodName, object[] args) { if (logger.IsEnabled(LogLevel.Trace)) { var argsList = args == null ? string.Empty : string.Join(", ", args.Select(a => a?.GetType().FullName ?? "(null)")); _issuingInvocation(logger, invocationId, returnType, methodName, argsList, null); } } public static void SendingMessage(ILogger logger, HubInvocationMessage message) { if (logger.IsEnabled(LogLevel.Debug)) { _sendingMessage(logger, message.GetType().Name, message.InvocationId, null); } } public static void MessageSent(ILogger logger, HubInvocationMessage message) { if (logger.IsEnabled(LogLevel.Debug)) { _messageSent(logger, message.GetType().Name, message.InvocationId, null); } } public static void FailedToSendInvocation(ILogger logger, string invocationId, Exception exception) { _failedToSendInvocation(logger, invocationId, exception); } public static void ReceivedInvocation(ILogger logger, string invocationId, string methodName, object[] args) { if (logger.IsEnabled(LogLevel.Trace)) { var argsList = args == null ? string.Empty : string.Join(", ", args.Select(a => a?.GetType().FullName ?? "(null)")); _receivedInvocation(logger, invocationId, methodName, argsList, null); } } public static void DroppedCompletionMessage(ILogger logger, string invocationId) { _droppedCompletionMessage(logger, invocationId, null); } public static void DroppedStreamMessage(ILogger logger, string invocationId) { _droppedStreamMessage(logger, invocationId, null); } public static void ShutdownConnection(ILogger logger) { _shutdownConnection(logger, null); } public static void ShutdownWithError(ILogger logger, Exception exception) { _shutdownWithError(logger, exception); } public static void RemovingInvocation(ILogger logger, string invocationId) { _removingInvocation(logger, invocationId, null); } public static void MissingHandler(ILogger logger, string target) { _missingHandler(logger, target, null); } public static void ReceivedStreamItem(ILogger logger, string invocationId) { _receivedStreamItem(logger, invocationId, null); } public static void CancelingStreamItem(ILogger logger, string invocationId) { _cancelingStreamItem(logger, invocationId, null); } public static void ReceivedStreamItemAfterClose(ILogger logger, string invocationId) { _receivedStreamItemAfterClose(logger, invocationId, null); } public static void ReceivedInvocationCompletion(ILogger logger, string invocationId) { _receivedInvocationCompletion(logger, invocationId, null); } public static void CancelingInvocationCompletion(ILogger logger, string invocationId) { _cancelingInvocationCompletion(logger, invocationId, null); } public static void Stopped(ILogger logger) { _stopped(logger, null); } public static void InvocationAlreadyInUse(ILogger logger, string invocationId) { _invocationAlreadyInUse(logger, invocationId, null); } public static void ReceivedUnexpectedResponse(ILogger logger, string invocationId) { _receivedUnexpectedResponse(logger, invocationId, null); } public static void HubProtocol(ILogger logger, string hubProtocol, int version) { _hubProtocol(logger, hubProtocol, version, null); } public static void ResettingKeepAliveTimer(ILogger logger) { _resettingKeepAliveTimer(logger, null); } public static void ErrorDuringClosedEvent(ILogger logger, Exception exception) { _errorDuringClosedEvent(logger, exception); } public static void SendingHubHandshake(ILogger logger) { _sendingHubHandshake(logger, null); } public static void ReceivedPing(ILogger logger) { _receivedPing(logger, null); } public static void ErrorInvokingClientSideMethod(ILogger logger, string methodName, Exception exception) { _errorInvokingClientSideMethod(logger, methodName, exception); } public static void ErrorReceivingHandshakeResponse(ILogger logger, Exception exception) { _errorProcessingHandshakeResponse(logger, exception); } public static void HandshakeServerError(ILogger logger, string error) { _handshakeServerError(logger, error, null); } public static void ReceivedClose(ILogger logger) { _receivedClose(logger, null); } public static void ReceivedCloseWithError(ILogger logger, string error) { _receivedCloseWithError(logger, error, null); } public static void HandshakeComplete(ILogger logger) { _handshakeComplete(logger, null); } public static void RegisteringHandler(ILogger logger, string methodName) { _registeringHandler(logger, methodName, null); } public static void Starting(ILogger logger) { _starting(logger, null); } public static void ErrorStartingConnection(ILogger logger, Exception ex) { _errorStartingConnection(logger, ex); } public static void Started(ILogger logger) { _started(logger, null); } public static void SendingCancellation(ILogger logger, string invocationId) { _sendingCancellation(logger, invocationId, null); } public static void CancelingOutstandingInvocations(ILogger logger) { _cancelingOutstandingInvocations(logger, null); } public static void ReceiveLoopStarting(ILogger logger) { _receiveLoopStarting(logger, null); } public static void StartingServerTimeoutTimer(ILogger logger, TimeSpan serverTimeout) { if (logger.IsEnabled(LogLevel.Debug)) { _startingServerTimeoutTimer(logger, serverTimeout.TotalMilliseconds, null); } } public static void NotUsingServerTimeout(ILogger logger) { _notUsingServerTimeout(logger, null); } public static void ServerDisconnectedWithError(ILogger logger, Exception ex) { _serverDisconnectedWithError(logger, ex); } public static void InvokingClosedEventHandler(ILogger logger) { _invokingClosedEventHandler(logger, null); } public static void Stopping(ILogger logger) { _stopping(logger, null); } public static void TerminatingReceiveLoop(ILogger logger) { _terminatingReceiveLoop(logger, null); } public static void WaitingForReceiveLoopToTerminate(ILogger logger) { _waitingForReceiveLoopToTerminate(logger, null); } public static void ProcessingMessage(ILogger logger, long length) { _processingMessage(logger, length, null); } public static void WaitingOnConnectionLock(ILogger logger, string memberName, string filePath, int lineNumber) { _waitingOnConnectionLock(logger, memberName, filePath, lineNumber, null); } public static void ReleasingConnectionLock(ILogger logger, string memberName, string filePath, int lineNumber) { _releasingConnectionLock(logger, memberName, filePath, lineNumber, null); } public static void UnableToSendCancellation(ILogger logger, string invocationId) { _unableToSendCancellation(logger, invocationId, null); } } } }