Home > Net >  Enable websocket logging
Enable websocket logging

Time:01-28

When looking at the reference source I saw WebSocketBase have lots of logging statements, but there's no obvious way to make it actually log. I'm hoping it could help me diagnose why my client doesn't respond to ping requests during file transfers from the server, so how do I enable the logging here?

CodePudding user response:

If you are on .Net Framework 4.x and are using an app.config (or web.config if you're hosting a site) you can configure a TraceSource named "System.Net.WebSockets", add an appropriate Switch level and a listener.

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.Net.WebSockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
   </sources>
    <switches>
      <add name="System.Net.WebSockets" value="Verbose"/>
    </switches>
    <sharedListeners>
      <add name="System.Net"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="network.log"
        traceOutputOptions="ProcessId, DateTime"
      />
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>
</configuration>

This is the type of output you can expect with the Verbose switch (I'm connecting to ws://qa.sockets.stackexchange.com/ here)

System.Net.WebSockets Verbose: 0 : [9428] Entering ClientWebSocket#45004109::.ctor()
System.Net.WebSockets Verbose: 0 : [9428] Exiting ClientWebSocket#45004109::.ctor() 
System.Net.WebSockets Information: 0 : [9428] Associating ClientWebSocket#45004109 with HttpWebRequest#21454193
System.Net.WebSockets Information: 0 : [14188] Associating ClientWebSocket#45004109 with HttpWebResponse#12547953
System.Net.WebSockets Verbose: 0 : [14188] Entering InternalClientWebSocket#31364015::Initialize(ReceiveBufferSize: 16384, SendBufferSize: 16384,  Protocols: , KeepAliveInterval: 00:00:30, innerStream: WebSocketConnectionStream#11429296, internalBuffer: WebSocketBuffer#41622463)
System.Net.WebSockets Information: 0 : [14188] Associating InternalClientWebSocket#31364015 with WebSocketConnectionStream#11429296
System.Net.WebSockets Information: 0 : [14188] Associating InternalClientWebSocket#31364015 with WebSocketBuffer#41622463
System.Net.WebSockets Information: 0 : [14188] Associating WebSocketConnectionStream#11429296 with WebSocketConnection#28379535
System.Net.WebSockets Verbose: 0 : [14188] Exiting InternalClientWebSocket#31364015::Initialize()   -> ReceiveBufferSize: 16384, SendBufferSize: 16384,  Protocols: , KeepAliveInterval: 00:00:30, innerStream: WebSocketConnectionStream#11429296, internalBuffer: WebSocketBuffer#41622463
System.Net.WebSockets Information: 0 : [14188] Associating ClientWebSocket#45004109 with InternalClientWebSocket#31364015
System.Net.WebSockets Verbose: 0 : [14188] Entering InternalClientWebSocket#31364015::SendAsync(messageType: Text, endOfMessage: True)
System.Net.WebSockets Verbose: 0 : [14188] Data from InternalClientWebSocket#31364015::SendAsync
System.Net.WebSockets Verbose: 0 : [14188] 00000000 : 31 35 35 2D 71 75 65 73-74 69 6F 6E 73 2D 61 63 : 155-questions-ac
System.Net.WebSockets Verbose: 0 : [14188] 00000010 : 74 69 76 65                                     : tive
System.Net.WebSockets Verbose: 0 : [14188] Entering WebSocketConnectionStream#11429296::WriteAsync(offset: 16384, count: 26, cancellationToken.CanBeCanceled: True)
System.Net.WebSockets Verbose: 0 : [14188] Entering WebSocketConnection#28379535::WriteAsync(offset: 16384, count: 26, cancellationToken.CanBeCanceled: True)
System.Net.WebSockets Verbose: 0 : [14188] Exiting WebSocketConnection#28379535::WriteAsync()   -> Boolean#1
System.Net.WebSockets Verbose: 0 : [3848] Entering WebSocketConnection#28379535::OnWriteCompleted(LastOperation: Send, SocketError: Success)
System.Net.WebSockets Verbose: 0 : [3848] Data from WebSocketConnectionStream#11429296::WriteAsync
System.Net.WebSockets Verbose: 0 : [3848] 00004000 : 81 94 2C 5F 76 52 1D 6A-43 7F 5D 2A 13 21 58 36 : ..,_vR.jC.]*.!X6
System.Net.WebSockets Verbose: 0 : [3848] 00004010 : 19 3C 5F 72 17 31 58 36-00 37                   : .<_r.1X6.7
System.Net.WebSockets Verbose: 0 : [3848] Exiting WebSocketConnectionStream#11429296::WriteAsync() 
System.Net.WebSockets Verbose: 0 : [3848] Exiting InternalClientWebSocket#31364015::SendAsync()     -> messageType: Text, endOfMessage: True
System.Net.WebSockets Verbose: 0 : [3848] Entering InternalClientWebSocket#31364015::ReceiveAsync()
System.Net.WebSockets Verbose: 0 : [3848] Entering WebSocketConnectionStream#11429296::ReadAsync(offset: 0, count: 16384, cancellationToken.CanBeCanceled: True)
System.Net.WebSockets Verbose: 0 : [3848] Entering WebSocketConnection#28379535::ReadAsyncCore(offset: 0, count: 16384, cancellationToken.CanBeCanceled: True)
System.Net.WebSockets Verbose: 0 : [3848] Exiting WebSocketConnection#28379535::ReadAsyncCore()     -> Boolean#1
System.Net.WebSockets Verbose: 0 : [3848] Exiting WebSocketConnection#28379535::OnWriteCompleted() 
System.Net.WebSockets Verbose: 0 : [3848] Entering WebSocketConnection#28379535::OnReadCompleted(LastOperation: Receive, SocketError: Success)
System.Net.WebSockets Verbose: 0 : [3848] Data from WebSocketConnectionStream#11429296::ReadAsync
System.Net.WebSockets Verbose: 0 : [3848] 00000000 : 81 7E 03 11 7B 22 61 63-74 69 6F 6E 22 3A 22 31 : .~..{"action":"1
System.Net.WebSockets Verbose: 0 : [3848] 00000010 : 35 35 2D 71 75 65 73 74-69 6F 6E 73 2D 61 63 74 : 55-questions-act
System.Net.WebSockets Verbose: 0 : [3848] 00000020 : 69 76 65 22 2C 22 64 61-74 61 22 3A 22 7B 5C 22 : ive","data":"{\"
System.Net.WebSockets Verbose: 0 : [3848] 00000030 : 73 69 74 65 42 61 73 65-48 6F 73 74 41 64 64 72 : siteBaseHostAddr
System.Net.WebSockets Verbose: 0 : [3848] 00000040 : 65 73 73 5C 22 3A 5C 22-73 74 61 63 6B 6F 76 65 : ess\":\"stackove
System.Net.WebSockets Verbose: 0 : [3848] 00000050 : 72 66 6C 6F 77 2E 63 6F-6D 5C 22 2C 5C 22 69 64 : rflow.com\",\"id
  • Related