// 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.Net.WebSockets; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Sockets.Client.Internal { internal static class SocketClientLoggerExtensions { // Category: Shared with LongPollingTransport, WebSocketsTransport and ServerSentEventsTransport private static readonly Action _startTransport = LoggerMessage.Define(LogLevel.Information, new EventId(0, nameof(StartTransport)), "{time}: Connection Id {connectionId}: Starting transport. Transfer mode: {transferMode}."); private static readonly Action _transportStopped = LoggerMessage.Define(LogLevel.Debug, new EventId(1, nameof(TransportStopped)), "{time}: Connection Id {connectionId}: Transport stopped."); private static readonly Action _startReceive = LoggerMessage.Define(LogLevel.Debug, new EventId(2, nameof(StartReceive)), "{time}: Connection Id {connectionId}: Starting receive loop."); private static readonly Action _receiveStopped = LoggerMessage.Define(LogLevel.Debug, new EventId(3, nameof(ReceiveStopped)), "{time}: Connection Id {connectionId}: Receive loop stopped."); private static readonly Action _receiveCanceled = LoggerMessage.Define(LogLevel.Debug, new EventId(4, nameof(ReceiveCanceled)), "{time}: Connection Id {connectionId}: Receive loop canceled."); private static readonly Action _transportStopping = LoggerMessage.Define(LogLevel.Information, new EventId(5, nameof(TransportStopping)), "{time}: Connection Id {connectionId}: Transport is stopping."); private static readonly Action _sendStarted = LoggerMessage.Define(LogLevel.Debug, new EventId(6, nameof(SendStarted)), "{time}: Connection Id {connectionId}: Starting the send loop."); private static readonly Action _sendStopped = LoggerMessage.Define(LogLevel.Debug, new EventId(7, nameof(SendStopped)), "{time}: Connection Id {connectionId}: Send loop stopped."); private static readonly Action _sendCanceled = LoggerMessage.Define(LogLevel.Debug, new EventId(8, nameof(SendCanceled)), "{time}: Connection Id {connectionId}: Send loop canceled."); // Category: WebSocketsTransport private static readonly Action _webSocketClosed = LoggerMessage.Define(LogLevel.Information, new EventId(9, nameof(WebSocketClosed)), "{time}: Connection Id {connectionId}: Websocket closed by the server. Close status {closeStatus}."); private static readonly Action _messageReceived = LoggerMessage.Define(LogLevel.Debug, new EventId(10, nameof(MessageReceived)), "{time}: Connection Id {connectionId}: Message received. Type: {messageType}, size: {count}, EndOfMessage: {endOfMessage}."); private static readonly Action _messageToApp = LoggerMessage.Define(LogLevel.Debug, new EventId(11, nameof(MessageToApp)), "{time}: Connection Id {connectionId}: Passing message to application. Payload size: {count}."); private static readonly Action _receivedFromApp = LoggerMessage.Define(LogLevel.Debug, new EventId(12, nameof(ReceivedFromApp)), "{time}: Connection Id {connectionId}: Received message from application. Payload size: {count}."); private static readonly Action _sendMessageCanceled = LoggerMessage.Define(LogLevel.Information, new EventId(13, nameof(SendMessageCanceled)), "{time}: Connection Id {connectionId}: Sending a message canceled."); private static readonly Action _errorSendingMessage = LoggerMessage.Define(LogLevel.Error, new EventId(14, nameof(ErrorSendingMessage)), "{time}: Connection Id {connectionId}: Error while sending a message."); private static readonly Action _closingWebSocket = LoggerMessage.Define(LogLevel.Information, new EventId(15, nameof(ClosingWebSocket)), "{time}: Connection Id {connectionId}: Closing WebSocket."); private static readonly Action _closingWebSocketFailed = LoggerMessage.Define(LogLevel.Information, new EventId(16, nameof(ClosingWebSocketFailed)), "{time}: Connection Id {connectionId}: Closing webSocket failed."); private static readonly Action _cancelMessage = LoggerMessage.Define(LogLevel.Debug, new EventId(17, nameof(CancelMessage)), "{time}: Connection Id {connectionId}: Canceled passing message to application."); // Category: ServerSentEventsTransport and LongPollingTransport private static readonly Action _sendingMessages = LoggerMessage.Define(LogLevel.Debug, new EventId(9, nameof(SendingMessages)), "{time}: Connection Id {connectionId}: Sending {count} message(s) to the server using url: {url}."); private static readonly Action _sentSuccessfully = LoggerMessage.Define(LogLevel.Debug, new EventId(10, nameof(SentSuccessfully)), "{time}: Connection Id {connectionId}: Message(s) sent successfully."); private static readonly Action _noMessages = LoggerMessage.Define(LogLevel.Debug, new EventId(11, nameof(NoMessages)), "{time}: Connection Id {connectionId}: No messages in batch to send."); private static readonly Action _errorSending = LoggerMessage.Define(LogLevel.Error, new EventId(12, nameof(ErrorSending)), "{time}: Connection Id {connectionId}: Error while sending to '{url}'."); // Category: ServerSentEventsTransport private static readonly Action _eventStreamEnded = LoggerMessage.Define(LogLevel.Debug, new EventId(13, nameof(EventStreamEnded)), "{time}: Connection Id {connectionId}: Server-Sent Event Stream ended."); // Category: LongPollingTransport private static readonly Action _closingConnection = LoggerMessage.Define(LogLevel.Debug, new EventId(13, nameof(ClosingConnection)), "{time}: Connection Id {connectionId}: The server is closing the connection."); private static readonly Action _receivedMessages = LoggerMessage.Define(LogLevel.Debug, new EventId(14, nameof(ReceivedMessages)), "{time}: Connection Id {connectionId}: Received messages from the server."); private static readonly Action _errorPolling = LoggerMessage.Define(LogLevel.Error, new EventId(15, nameof(ErrorPolling)), "{time}: Connection Id {connectionId}: Error while polling '{pollUrl}'."); // Category: HttpConnection private static readonly Action _httpConnectionStarting = LoggerMessage.Define(LogLevel.Debug, new EventId(0, nameof(HttpConnectionStarting)), "{time}: Starting connection."); private static readonly Action _httpConnectionClosed = LoggerMessage.Define(LogLevel.Debug, new EventId(1, nameof(HttpConnectionClosed)), "{time}: Connection Id {connectionId}: Connection was closed from a different thread."); private static readonly Action _startingTransport = LoggerMessage.Define(LogLevel.Debug, new EventId(2, nameof(StartingTransport)), "{time}: Connection Id {connectionId}: Starting transport '{transport}' with Url: {url}."); private static readonly Action _raiseConnected = LoggerMessage.Define(LogLevel.Debug, new EventId(3, nameof(RaiseConnected)), "{time}: Connection Id {connectionId}: Raising Connected event."); private static readonly Action _processRemainingMessages = LoggerMessage.Define(LogLevel.Debug, new EventId(4, nameof(ProcessRemainingMessages)), "{time}: Connection Id {connectionId}: Ensuring all outstanding messages are processed."); private static readonly Action _drainEvents = LoggerMessage.Define(LogLevel.Debug, new EventId(5, nameof(DrainEvents)), "{time}: Connection Id {connectionId}: Draining event queue."); private static readonly Action _raiseClosed = LoggerMessage.Define(LogLevel.Debug, new EventId(6, nameof(RaiseClosed)), "{time}: Connection Id {connectionId}: Raising Closed event."); private static readonly Action _establishingConnection = LoggerMessage.Define(LogLevel.Debug, new EventId(7, nameof(EstablishingConnection)), "{time}: Establishing Connection at: {url}."); private static readonly Action _errorWithNegotiation = LoggerMessage.Define(LogLevel.Error, new EventId(8, nameof(ErrorWithNegotiation)), "{time}: Failed to start connection. Error getting negotiation response from '{url}'."); private static readonly Action _errorStartingTransport = LoggerMessage.Define(LogLevel.Error, new EventId(9, nameof(ErrorStartingTransport)), "{time}: Connection Id {connectionId}: Failed to start connection. Error starting transport '{transport}'."); private static readonly Action _httpReceiveStarted = LoggerMessage.Define(LogLevel.Trace, new EventId(10, nameof(HttpReceiveStarted)), "{time}: Connection Id {connectionId}: Beginning receive loop."); private static readonly Action _skipRaisingReceiveEvent = LoggerMessage.Define(LogLevel.Debug, new EventId(11, nameof(SkipRaisingReceiveEvent)), "{time}: Connection Id {connectionId}: Message received but connection is not connected. Skipping raising Received event."); private static readonly Action _scheduleReceiveEvent = LoggerMessage.Define(LogLevel.Debug, new EventId(12, nameof(ScheduleReceiveEvent)), "{time}: Connection Id {connectionId}: Scheduling raising Received event."); private static readonly Action _raiseReceiveEvent = LoggerMessage.Define(LogLevel.Debug, new EventId(13, nameof(RaiseReceiveEvent)), "{time}: Connection Id {connectionId}: Raising Received event."); private static readonly Action _failedReadingMessage = LoggerMessage.Define(LogLevel.Debug, new EventId(14, nameof(FailedReadingMessage)), "{time}: Connection Id {connectionId}: Could not read message."); private static readonly Action _errorReceiving = LoggerMessage.Define(LogLevel.Error, new EventId(15, nameof(ErrorReceiving)), "{time}: Connection Id {connectionId}: Error receiving message."); private static readonly Action _endReceive = LoggerMessage.Define(LogLevel.Trace, new EventId(16, nameof(EndReceive)), "{time}: Connection Id {connectionId}: Ending receive loop."); private static readonly Action _sendingMessage = LoggerMessage.Define(LogLevel.Debug, new EventId(17, nameof(SendingMessage)), "{time}: Connection Id {connectionId}: Sending message."); private static readonly Action _stoppingClient = LoggerMessage.Define(LogLevel.Information, new EventId(18, nameof(StoppingClient)), "{time}: Connection Id {connectionId}: Stopping client."); private static readonly Action _exceptionThrownFromHandler = LoggerMessage.Define(LogLevel.Error, new EventId(19, nameof(ExceptionThrownFromHandler)), "{time}: Connection Id {connectionId}: An exception was thrown from the '{eventHandlerName}' event handler."); public static void StartTransport(this ILogger logger, string connectionId, TransferMode transferMode) { if (logger.IsEnabled(LogLevel.Information)) { _startTransport(logger, DateTime.Now, connectionId, transferMode, null); } } public static void TransportStopped(this ILogger logger, string connectionId, Exception exception) { if (logger.IsEnabled(LogLevel.Debug)) { _transportStopped(logger, DateTime.Now, connectionId, exception); } } public static void StartReceive(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _startReceive(logger, DateTime.Now, connectionId, null); } } public static void TransportStopping(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Information)) { _transportStopping(logger, DateTime.Now, connectionId, null); } } public static void WebSocketClosed(this ILogger logger, string connectionId, WebSocketCloseStatus? closeStatus) { if (logger.IsEnabled(LogLevel.Information)) { _webSocketClosed(logger, DateTime.Now, connectionId, closeStatus, null); } } public static void MessageReceived(this ILogger logger, string connectionId, WebSocketMessageType messageType, int count, bool endOfMessage) { if (logger.IsEnabled(LogLevel.Debug)) { _messageReceived(logger, DateTime.Now, connectionId, messageType, count, endOfMessage, null); } } public static void MessageToApp(this ILogger logger, string connectionId, int count) { if (logger.IsEnabled(LogLevel.Debug)) { _messageToApp(logger, DateTime.Now, connectionId, count, null); } } public static void ReceiveCanceled(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _receiveCanceled(logger, DateTime.Now, connectionId, null); } } public static void ReceiveStopped(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _receiveStopped(logger, DateTime.Now, connectionId, null); } } public static void SendStarted(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _sendStarted(logger, DateTime.Now, connectionId, null); } } public static void ReceivedFromApp(this ILogger logger, string connectionId, int count) { if (logger.IsEnabled(LogLevel.Debug)) { _receivedFromApp(logger, DateTime.Now, connectionId, count, null); } } public static void SendMessageCanceled(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Information)) { _sendMessageCanceled(logger, DateTime.Now, connectionId, null); } } public static void ErrorSendingMessage(this ILogger logger, string connectionId, Exception exception) { if (logger.IsEnabled(LogLevel.Error)) { _errorSendingMessage(logger, DateTime.Now, connectionId, exception); } } public static void SendCanceled(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _sendCanceled(logger, DateTime.Now, connectionId, null); } } public static void SendStopped(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _sendStopped(logger, DateTime.Now, connectionId, null); } } public static void ClosingWebSocket(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Information)) { _closingWebSocket(logger, DateTime.Now, connectionId, null); } } public static void ClosingWebSocketFailed(this ILogger logger, string connectionId, Exception exception) { if (logger.IsEnabled(LogLevel.Information)) { _closingWebSocketFailed(logger, DateTime.Now, connectionId, exception); } } public static void CancelMessage(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _cancelMessage(logger, DateTime.Now, connectionId, null); } } public static void SendingMessages(this ILogger logger, string connectionId, int count, Uri url) { if (logger.IsEnabled(LogLevel.Debug)) { _sendingMessages(logger, DateTime.Now, connectionId, count, url, null); } } public static void SentSuccessfully(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _sentSuccessfully(logger, DateTime.Now, connectionId, null); } } public static void NoMessages(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _noMessages(logger, DateTime.Now, connectionId, null); } } public static void ErrorSending(this ILogger logger, string connectionId, Uri url, Exception exception) { if (logger.IsEnabled(LogLevel.Error)) { _errorSending(logger, DateTime.Now, connectionId, url, exception); } } public static void EventStreamEnded(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _eventStreamEnded(logger, DateTime.Now, connectionId, null); } } public static void ClosingConnection(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _closingConnection(logger, DateTime.Now, connectionId, null); } } public static void ReceivedMessages(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _receivedMessages(logger, DateTime.Now, connectionId, null); } } public static void ErrorPolling(this ILogger logger, string connectionId, Uri pollUrl, Exception exception) { if (logger.IsEnabled(LogLevel.Error)) { _errorPolling(logger, DateTime.Now, connectionId, pollUrl, exception); } } public static void HttpConnectionStarting(this ILogger logger) { if (logger.IsEnabled(LogLevel.Debug)) { _httpConnectionStarting(logger, DateTime.Now, null); } } public static void HttpConnectionClosed(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _httpConnectionClosed(logger, DateTime.Now, connectionId, null); } } public static void StartingTransport(this ILogger logger, string connectionId, string transport, Uri url) { if (logger.IsEnabled(LogLevel.Debug)) { _startingTransport(logger, DateTime.Now, connectionId, transport, url, null); } } public static void RaiseConnected(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _raiseConnected(logger, DateTime.Now, connectionId, null); } } public static void ProcessRemainingMessages(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _processRemainingMessages(logger, DateTime.Now, connectionId, null); } } public static void DrainEvents(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _drainEvents(logger, DateTime.Now, connectionId, null); } } public static void RaiseClosed(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _raiseClosed(logger, DateTime.Now, connectionId, null); } } public static void EstablishingConnection(this ILogger logger, Uri url) { if (logger.IsEnabled(LogLevel.Debug)) { _establishingConnection(logger, DateTime.Now, url, null); } } public static void ErrorWithNegotiation(this ILogger logger, Uri url, Exception exception) { if (logger.IsEnabled(LogLevel.Error)) { _errorWithNegotiation(logger, DateTime.Now, url, exception); } } public static void ErrorStartingTransport(this ILogger logger, string connectionId, string transport, Exception exception) { if (logger.IsEnabled(LogLevel.Error)) { _errorStartingTransport(logger, DateTime.Now, connectionId, transport, exception); } } public static void HttpReceiveStarted(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Trace)) { _httpReceiveStarted(logger, DateTime.Now, connectionId, null); } } public static void SkipRaisingReceiveEvent(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _skipRaisingReceiveEvent(logger, DateTime.Now, connectionId, null); } } public static void ScheduleReceiveEvent(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _scheduleReceiveEvent(logger, DateTime.Now, connectionId, null); } } public static void RaiseReceiveEvent(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _raiseReceiveEvent(logger, DateTime.Now, connectionId, null); } } public static void FailedReadingMessage(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _failedReadingMessage(logger, DateTime.Now, connectionId, null); } } public static void ErrorReceiving(this ILogger logger, string connectionId, Exception exception) { if (logger.IsEnabled(LogLevel.Error)) { _errorReceiving(logger, DateTime.Now, connectionId, exception); } } public static void EndReceive(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Trace)) { _endReceive(logger, DateTime.Now, connectionId, null); } } public static void SendingMessage(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Debug)) { _sendingMessage(logger, DateTime.Now, connectionId, null); } } public static void StoppingClient(this ILogger logger, string connectionId) { if (logger.IsEnabled(LogLevel.Information)) { _stoppingClient(logger, DateTime.Now, connectionId, null); } } public static void ExceptionThrownFromHandler(this ILogger logger, string connectionId, string eventHandlerName, Exception exception) { if (logger.IsEnabled(LogLevel.Error)) { _exceptionThrownFromHandler(logger, DateTime.Now, connectionId, eventHandlerName, exception); } } } }