16
16
17
17
namespace Orleans . Runtime . Messaging
18
18
{
19
- internal abstract class Connection
19
+ internal abstract partial class Connection
20
20
{
21
21
private static readonly Func < ConnectionContext , Task > OnConnectedDelegate = context => OnConnectedAsync ( context ) ;
22
22
private static readonly Action < object > OnConnectionClosedDelegate = state => ( ( Connection ) state ) . OnTransportConnectionClosed ( ) ;
@@ -152,13 +152,7 @@ private void StartClosing(Exception exception)
152
152
_initializationTcs . TrySetException ( exception ?? new ConnectionAbortedException ( "Connection initialization failed" ) ) ;
153
153
_initializationTcs . Task . Ignore ( ) ;
154
154
155
- if ( this . Log . IsEnabled ( LogLevel . Information ) )
156
- {
157
- this . Log . LogInformation (
158
- exception ,
159
- "Closing connection {Connection}" ,
160
- this ) ;
161
- }
155
+ LogInformationClosingConnection ( this . Log , exception , this ) ;
162
156
163
157
task . Start ( TaskScheduler . Default ) ;
164
158
}
@@ -188,7 +182,7 @@ private async Task CloseAsync()
188
182
catch ( Exception processIncomingException )
189
183
{
190
184
// Swallow any exceptions here.
191
- this . Log . LogWarning ( processIncomingException , "Exception processing incoming messages on connection {Connection}" , this ) ;
185
+ LogWarningExceptionProcessingIncomingMessages ( this . Log , processIncomingException , this ) ;
192
186
}
193
187
}
194
188
@@ -201,7 +195,7 @@ private async Task CloseAsync()
201
195
catch ( Exception processOutgoingException )
202
196
{
203
197
// Swallow any exceptions here.
204
- this . Log . LogWarning ( processOutgoingException , "Exception processing outgoing messages on connection {Connection}" , this ) ;
198
+ LogWarningExceptionProcessingOutgoingMessages ( this . Log , processOutgoingException , this ) ;
205
199
}
206
200
}
207
201
@@ -215,7 +209,7 @@ private async Task CloseAsync()
215
209
}
216
210
catch ( Exception exception )
217
211
{
218
- this . Log . LogWarning ( exception , "Exception aborting connection {Connection}" , this ) ;
212
+ LogWarningExceptionAbortingConnection ( this . Log , exception , this ) ;
219
213
}
220
214
221
215
await _transportConnectionClosed . Task ;
@@ -228,7 +222,7 @@ private async Task CloseAsync()
228
222
catch ( Exception abortException )
229
223
{
230
224
// Swallow any exceptions here.
231
- this . Log . LogWarning ( abortException , "Exception terminating connection {Connection}" , this ) ;
225
+ LogWarningExceptionTerminatingConnection ( this . Log , abortException , this ) ;
232
226
}
233
227
234
228
// Reject in-flight messages.
@@ -243,23 +237,18 @@ private async Task CloseAsync()
243
237
var i = 0 ;
244
238
while ( this . outgoingMessages . Reader . TryRead ( out var message ) )
245
239
{
246
- if ( i == 0 && Log . IsEnabled ( LogLevel . Information ) )
240
+ if ( i == 0 )
247
241
{
248
- this . Log . LogInformation (
249
- "Rerouting messages for remote endpoint {EndPoint}" ,
250
- this . RemoteEndPoint ? . ToString ( ) ?? "(never connected)" ) ;
242
+ LogInformationReroutingMessages ( this . Log , new EndPointLogValue ( this . RemoteEndPoint ) ) ;
251
243
}
252
244
253
245
++ i ;
254
246
this . RetryMessage ( message ) ;
255
247
}
256
248
257
- if ( i > 0 && this . Log . IsEnabled ( LogLevel . Information ) )
249
+ if ( i > 0 )
258
250
{
259
- this . Log . LogInformation (
260
- "Rerouted {Count} messages for remote endpoint {EndPoint}" ,
261
- i ,
262
- this . RemoteEndPoint ? . ToString ( ) ?? "(never connected)" ) ;
251
+ LogInformationReroutedMessages ( this . Log , i , new EndPointLogValue ( this . RemoteEndPoint ) ) ;
263
252
}
264
253
}
265
254
@@ -334,10 +323,7 @@ private async Task ProcessIncoming()
334
323
{
335
324
if ( IsValid )
336
325
{
337
- this . Log . LogWarning (
338
- exception ,
339
- "Exception while processing messages from remote endpoint {EndPoint}" ,
340
- this . RemoteEndPoint ) ;
326
+ LogWarningExceptionProcessingMessagesFromRemote ( this . Log , exception , this . RemoteEndPoint ) ;
341
327
}
342
328
343
329
error = exception ;
@@ -402,10 +388,7 @@ private async Task ProcessOutgoing()
402
388
{
403
389
if ( IsValid )
404
390
{
405
- this . Log . LogWarning (
406
- exception ,
407
- "Exception while processing messages to remote endpoint {EndPoint}" ,
408
- this . RemoteEndPoint ) ;
391
+ LogWarningExceptionProcessingMessagesToRemote ( this . Log , exception , this . RemoteEndPoint ) ;
409
392
}
410
393
411
394
error = exception ;
@@ -419,13 +402,7 @@ private async Task ProcessOutgoing()
419
402
420
403
private void RerouteMessage ( Message message )
421
404
{
422
- if ( this . Log . IsEnabled ( LogLevel . Information ) )
423
- {
424
- this . Log . LogInformation (
425
- "Rerouting message {Message} from remote endpoint {EndPoint}" ,
426
- message ,
427
- this . RemoteEndPoint ? . ToString ( ) ?? "(never connected)" ) ;
428
- }
405
+ LogInformationReroutingMessage ( this . Log , message , new EndPointLogValue ( this . RemoteEndPoint ) ) ;
429
406
430
407
ThreadPool . UnsafeQueueUserWorkItem ( state =>
431
408
{
@@ -453,12 +430,7 @@ private static EndPoint NormalizeEndpoint(EndPoint endpoint)
453
430
/// <returns><see langword="true"/> if the exception should not be caught and <see langword="false"/> if it should be caught.</returns>
454
431
private bool HandleReceiveMessageFailure ( Message message , Exception exception )
455
432
{
456
- this . Log . LogError (
457
- exception ,
458
- "Exception reading message {Message} from remote endpoint {Remote} to local endpoint {Local}" ,
459
- message ,
460
- this . RemoteEndPoint ,
461
- this . LocalEndPoint ) ;
433
+ LogErrorExceptionReadingMessage ( this . Log , exception , message , this . RemoteEndPoint , this . LocalEndPoint ) ;
462
434
463
435
// If deserialization completely failed, rethrow the exception so that it can be handled at another level.
464
436
if ( message is null || exception is InvalidMessageFrameException )
@@ -500,12 +472,7 @@ private bool HandleSendMessageFailure(Message message, Exception exception)
500
472
// We get here if we failed to serialize the msg (or any other catastrophic failure).
501
473
// Request msg fails to serialize on the sender, so we just enqueue a rejection msg.
502
474
// Response msg fails to serialize on the responding silo, so we try to send an error response back.
503
- this . Log . LogError (
504
- exception ,
505
- "Exception sending message {Message} to remote endpoint {Remote} from local endpoint {Local}" ,
506
- message ,
507
- this . RemoteEndPoint ,
508
- this . LocalEndPoint ) ;
475
+ LogErrorExceptionSendingMessage ( this . Log , exception , message , this . RemoteEndPoint , this . LocalEndPoint ) ;
509
476
510
477
if ( message is null || exception is InvalidMessageFrameException )
511
478
{
@@ -535,10 +502,9 @@ private bool HandleSendMessageFailure(Message message, Exception exception)
535
502
}
536
503
else
537
504
{
538
- this . Log . LogWarning (
539
- ( int ) ErrorCode . Messaging_OutgoingMS_DroppingMessage ,
505
+ LogWarningDroppingMessage (
506
+ this . Log ,
540
507
exception ,
541
- "Dropping message which failed during serialization: {Message}" ,
542
508
message ) ;
543
509
544
510
MessagingInstruments . OnDroppedSentMessage ( message ) ;
@@ -581,5 +547,89 @@ public void Reset()
581
547
this . connection = null ;
582
548
}
583
549
}
550
+
551
+ private readonly struct EndPointLogValue ( EndPoint endPoint )
552
+ {
553
+ public override string ToString ( ) => endPoint ? . ToString ( ) ?? "(never connected)" ;
554
+ }
555
+
556
+ [ LoggerMessage (
557
+ Level = LogLevel . Information ,
558
+ Message = "Closing connection {Connection}"
559
+ ) ]
560
+ private static partial void LogInformationClosingConnection ( ILogger logger , Exception exception , Connection connection ) ;
561
+
562
+ [ LoggerMessage (
563
+ Level = LogLevel . Warning ,
564
+ Message = "Exception processing incoming messages on connection {Connection}"
565
+ ) ]
566
+ private static partial void LogWarningExceptionProcessingIncomingMessages ( ILogger logger , Exception exception , Connection connection ) ;
567
+
568
+ [ LoggerMessage (
569
+ Level = LogLevel . Warning ,
570
+ Message = "Exception processing outgoing messages on connection {Connection}"
571
+ ) ]
572
+ private static partial void LogWarningExceptionProcessingOutgoingMessages ( ILogger logger , Exception exception , Connection connection ) ;
573
+
574
+ [ LoggerMessage (
575
+ Level = LogLevel . Warning ,
576
+ Message = "Exception aborting connection {Connection}"
577
+ ) ]
578
+ private static partial void LogWarningExceptionAbortingConnection ( ILogger logger , Exception exception , Connection connection ) ;
579
+
580
+ [ LoggerMessage (
581
+ Level = LogLevel . Warning ,
582
+ Message = "Exception terminating connection {Connection}"
583
+ ) ]
584
+ private static partial void LogWarningExceptionTerminatingConnection ( ILogger logger , Exception exception , Connection connection ) ;
585
+
586
+ [ LoggerMessage (
587
+ Level = LogLevel . Information ,
588
+ Message = "Rerouting messages for remote endpoint {EndPoint}"
589
+ ) ]
590
+ private static partial void LogInformationReroutingMessages ( ILogger logger , EndPointLogValue endPoint ) ;
591
+
592
+ [ LoggerMessage (
593
+ Level = LogLevel . Information ,
594
+ Message = "Rerouted {Count} messages for remote endpoint {EndPoint}"
595
+ ) ]
596
+ private static partial void LogInformationReroutedMessages ( ILogger logger , int count , EndPointLogValue endPoint ) ;
597
+
598
+ [ LoggerMessage (
599
+ Level = LogLevel . Warning ,
600
+ Message = "Exception while processing messages from remote endpoint {EndPoint}"
601
+ ) ]
602
+ private static partial void LogWarningExceptionProcessingMessagesFromRemote ( ILogger logger , Exception exception , EndPoint endPoint ) ;
603
+
604
+ [ LoggerMessage (
605
+ Level = LogLevel . Warning ,
606
+ Message = "Exception while processing messages to remote endpoint {EndPoint}"
607
+ ) ]
608
+ private static partial void LogWarningExceptionProcessingMessagesToRemote ( ILogger logger , Exception exception , EndPoint endPoint ) ;
609
+
610
+ [ LoggerMessage (
611
+ Level = LogLevel . Information ,
612
+ Message = "Rerouting message {Message} from remote endpoint {EndPoint}"
613
+ ) ]
614
+ private static partial void LogInformationReroutingMessage ( ILogger logger , Message message , EndPointLogValue endPoint ) ;
615
+
616
+ [ LoggerMessage (
617
+ Level = LogLevel . Error ,
618
+ Message = "Exception reading message {Message} from remote endpoint {Remote} to local endpoint {Local}"
619
+ ) ]
620
+ private static partial void LogErrorExceptionReadingMessage ( ILogger logger , Exception exception , Message message , EndPoint remote , EndPoint local ) ;
621
+
622
+ [ LoggerMessage (
623
+ Level = LogLevel . Error ,
624
+ Message = "Exception sending message {Message} to remote endpoint {Remote} from local endpoint {Local}"
625
+ ) ]
626
+ private static partial void LogErrorExceptionSendingMessage ( ILogger logger , Exception exception , Message message , EndPoint remote , EndPoint local ) ;
627
+
628
+ [ LoggerMessage (
629
+ EventId = ( int ) ErrorCode . Messaging_OutgoingMS_DroppingMessage ,
630
+ Level = LogLevel . Warning ,
631
+ Message = "Dropping message which failed during serialization: {Message}"
632
+ ) ]
633
+ private static partial void LogWarningDroppingMessage ( ILogger logger , Exception exception , Message message ) ;
584
634
}
585
635
}
0 commit comments