Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable network tracing in dotnet core #64977

Closed
Zqasim132 opened this issue Feb 8, 2022 · 3 comments
Closed

Enable network tracing in dotnet core #64977

Zqasim132 opened this issue Feb 8, 2022 · 3 comments
Milestone

Comments

@Zqasim132
Copy link

I am writing a dotnet core console application that uses the HttpClient to make an HTTPS request that requires a client certificate. In .Net I can enable system diagnostics in the app.config (or web.config) or by getting the logging object at run time through System.Net.Logging namespace and it will output lots of information about the SSL connection. Is there a similar functionality in dotnet core?
I have seen the Tracing in .NET 6 but its not giving us the enough information such as wich cipher is used and what are the public and private key for the data encyption etc.
https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-6/#tracing

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net untriaged New issue has not been triaged by the area owner labels Feb 8, 2022
@ghost
Copy link

ghost commented Feb 8, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

I am writing a dotnet core console application that uses the HttpClient to make an HTTPS request that requires a client certificate. In .Net I can enable system diagnostics in the app.config (or web.config) or by getting the logging object at run time through System.Net.Logging namespace and it will output lots of information about the SSL connection. Is there a similar functionality in dotnet core?
I have seen the Tracing in .NET 6 but its not giving us the enough information such as wich cipher is used and what are the public and private key for the data encyption etc.
https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-6/#tracing

Author: Zqasim132
Assignees: -
Labels:

area-System.Net, untriaged

Milestone: -

@ManickaP
Copy link
Member

ManickaP commented Feb 8, 2022

If you want to get the internal logging, then it's a little bit more involved with .NET Core and newer.
You'd need create EventListener, subscribe to "Private.InternalDiagnostics.System.Net.*" where * might be: Http, Security, Sockets etc. (You can search our code for [EventSource(Name = "Private.InternalDiagnostics.System.Net.) and then log the messages to file/console/...
Please note that this logging is just for detailed diagnostics, is very verbose, messages might not be always helpful, etc...

Example:

using System.Diagnostics;
using System.Diagnostics.Tracing;

internal sealed class HttpEventListener : EventListener
{
    protected override void OnEventSourceCreated(EventSource eventSource)
    {
        // Allow internal HTTP logging
        if (eventSource.Name == "Private.InternalDiagnostics.System.Net.Http")
        {
            EnableEvents(eventSource, EventLevel.LogAlways);
        }
    }

    protected override void OnEventWritten(EventWrittenEventArgs eventData)
    {
        // Log whatever other properties you want, this is just an example
        var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.fffffff}[{eventData.EventName}] ");
        for (int i = 0; i < eventData.Payload?.Count; i++)
        {
            if (i > 0)
                sb.Append(", ");
            sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]);
        }
        try {
            Console.WriteLine(sb.ToString());
        } catch { }
    }
}

// Keep the listener around while you want the logging to continue, dispose it after.
var listener = new HttpEventListener(_output);

@Zqasim132
Copy link
Author

Sockets

Thanks
its worked for me

@ghost ghost locked as resolved and limited conversation to collaborators Mar 10, 2022
@karelz karelz added this to the 7.0.0 milestone Apr 8, 2022
@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants