@@ -62,7 +62,7 @@ public async Task WriteMessage(Message messageToWrite)
62
62
// the log level is Diagnostic where JsonRpc message payloads are logged and vary
63
63
// in size from 1K up to 225K chars. When not logging message payloads, the typical
64
64
// response log message size is under 256 chars.
65
- var logStrBld =
65
+ var logStrBld =
66
66
new StringBuilder ( this . logger . MinimumConfiguredLogLevel == LogLevel . Diagnostic ? 4096 : 256 )
67
67
. Append ( "Writing " )
68
68
. Append ( messageToWrite . MessageType )
@@ -76,7 +76,7 @@ public async Task WriteMessage(Message messageToWrite)
76
76
if ( this . logger . MinimumConfiguredLogLevel == LogLevel . Diagnostic )
77
77
{
78
78
// Log the JSON representation of the message payload at the Diagnostic log level
79
- string jsonPayload =
79
+ string jsonPayload =
80
80
JsonConvert . SerializeObject (
81
81
messageObject ,
82
82
Formatting . Indented ,
@@ -104,10 +104,20 @@ public async Task WriteMessage(Message messageToWrite)
104
104
// message loop doesn't get blocked while waiting for I/O to complete.
105
105
using ( await this . writeLock . LockAsync ( ) )
106
106
{
107
- // Send the message
108
- await this . outputStream . WriteAsync ( headerBytes , 0 , headerBytes . Length ) ;
109
- await this . outputStream . WriteAsync ( messageBytes , 0 , messageBytes . Length ) ;
110
- await this . outputStream . FlushAsync ( ) ;
107
+ try
108
+ {
109
+ // Send the message
110
+ await this . outputStream . WriteAsync ( headerBytes , 0 , headerBytes . Length ) ;
111
+ await this . outputStream . WriteAsync ( messageBytes , 0 , messageBytes . Length ) ;
112
+ await this . outputStream . FlushAsync ( ) ;
113
+ }
114
+ catch ( Exception e ) when (
115
+ e is ObjectDisposedException ||
116
+ e is IOException )
117
+ {
118
+ // We catch this exception for when the DebugAdapter disconnects while still processing a message.
119
+ logger . WriteHandledException ( "Tried to write to the output stream but it was already closed & broken." , e ) ;
120
+ }
111
121
}
112
122
}
113
123
0 commit comments