How to trace Websocket traffic with IIS

1.6k views Asked by At

I'm using IIS 10 and need to trace the traffic flowing in and out from the .NET 4.5.2 WebSocket that is created when someone opens a websocket to the server, this seems to be quite impossible.

The current trace configuration is logging System.Net, System.Net.Http, System.Net.Sockets and System.Net.WebSockets.

These are written to a NLog target. Whenever the server sends or receives data from other sockets, like Redis or Azure Storage, i get traces in my log.

However when a client connects to the server, i get http trace when opening the WebSocket, but no trace of data between the client and server.

The question is basically, does IIS actually write anything to tracing regarding the AspNetWebSocketContext.WebSocket ? If so, how can i log this data?

To clarify my configuration, Nlog config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="false" autoReload="true">
    <extensions>
        <add assembly="NLog.Target.TimeRoll" />
    </extensions>
    <targets>
        <target xsi:type="File" name="diag" fileName="${basedir}/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring}" />
        <target xsi:type="TimeRoll" name="timedTarget" Limit="30" fileName="\Timed_" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring}" />
  </targets>
  <rules>
        <logger name="*" minlevel="Trace" writeTo="timedTarget,diag">
  </rules>
</nlog>

And the relevant part of the web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
        <trace>
            <listeners>
                <add name="nlog" type="NLog.NLogTraceListener, NLog"></add>
            </listeners>
        </trace>
        <sources>
            <source name="System.Net" switchValue="All" tracemode="includehex" maxdatasize="250">
                <listeners>
                    <add name="nlog" />
                </listeners>
            </source>
            <source name="System.Net.Http" switchValue="All" tracemode="includehex" maxdatasize="250">
                <listeners> 
                    <add name="nlog" />
                </listeners>
            </source>
            <source name="System.Net.Sockets" switchValue="All" tracemode="includehex" maxdatasize="250">
                <listeners>
                    <add name="nlog" />
                </listeners>
            </source>
            <source name="System.Net.WebSockets" switchValue="All"  tracemode="includehex" maxdatasize="250">
                <listeners>
                    <add name="nlog" />
                </listeners>
            </source>
        </sources>
        <switches>
        </switches>
        <sharedListeners>
            <add name="nlog" type="NLog.NLogTraceListener, NLog" />
        </sharedListeners>
    </system.diagnostics>
</configuration>
0

There are 0 answers