I'm trying to log all outgoing HTTP requests and responses a .NET application makes. I am following this article from the Microsoft docs: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-configure-network-tracing
I am providing a short example of my code and app.config.
The following works as expected and produces a file network.log
which includes the full request and response body. However this only works on Windows and .NET Framework. Running the same code on Linux using mono version 5.18.1.0 - mono Program.exe
does not create the network.log
file and does not throw any exception.
C# Code:
using System;
using System.IO;
using System.Net;
namespace TraceTest
{
class Program
{
static void Main(string[] args)
{
WebRequest request = HttpWebRequest.Create("http://example.com");
using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader streamReader = new StreamReader(responseStream))
{
Console.WriteLine("Received: " + streamReader.ReadToEnd());
}
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<system.diagnostics>
<sources>
<source name="System.Net" tracemode="includehex" maxdatasize="1024">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<switches>
<add name="System.Net" 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>
Is there any way to log all network traffic on Mono from the application itself? I do not want to run an external program to capture the traffic.