'C# retrieve Received and Sent Bytes with TraceEvent - Network Activity for own .exe not working
I'm building a small tool for me. I wanted to retrieve only Bytes Received and Bytes Sent from my exe.
This is working code for "chrome", but if i change "chrome" with my executable file name (example: this is my bot) then it isnt working. The exe process gets found with the correct ID, but nothing more, its not counting ( 0 bytes both sides)...
And yes, the Code is running with Admin rights.
Edit: the code is running inside of my tool, so not a second exe.
internal static int received = 0;
internal static int sent = 0;
public static async Task test()
{
var counterLock = new object();
var processList = Process.GetProcessesByName("chrome").Select(p => p.Id).ToHashSet();
Task.Run(() =>
{
using (var session = new TraceEventSession("MyKernelAndClrEventsSession"))
{
session.EnableKernelProvider(KernelTraceEventParser.Keywords.NetworkTCPIP);
session.Source.Kernel.TcpIpRecv += data =>
{
if (processList.Contains(data.ProcessID))
lock (counterLock)
received += data.size;
};
session.Source.Kernel.TcpIpSend += data =>
{
if (processList.Contains(data.ProcessID))
lock (counterLock)
sent += data.size;
};
session.Source.Kernel.UdpIpRecv += data =>
{
if (processList.Contains(data.ProcessID))
lock (counterLock)
received += data.size;
};
session.Source.Kernel.UdpIpSend += data =>
{
if (processList.Contains(data.ProcessID))
lock (counterLock)
sent += data.size;
};
session.Source.Process();
}
});
while (true)
{
await Task.Delay(1000);
lock (counterLock)
Console.WriteLine($"Sent: {sent.ToString("N0")} bytes Received: {received.ToString("N0")} bytes");
}
}
What is wrong and why is my code not working for my own exe file but it works for other exe files like chrome?
Working for Chrome: Sent: 425 bytes Received: 80’750 bytes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
