简体   繁体   中英

How to add Socket and WinAPI tracing to .NET Core console application?

In classic .NET app, I did this by adding the following in app.config:

<system.diagnostics>
<sources>
  <source name="System.Net" tracemode="includehex" maxdatasize="1024">
    <listeners>
      <add name="System.Net"/>
    </listeners>
  </source>
  <source name="System.Net.Cache">
    <listeners>
      <add name="System.Net"/>
    </listeners>
  </source>
  <source name="System.Net.Http">
    <listeners>
      <add name="System.Net"/>
    </listeners>
  </source>
  <source name="System.Net.Sockets">
    <listeners>
      <add name="System.Net"/>
    </listeners>
  </source>
  <source name="System.Net.WebSockets">
    <listeners>
      <add name="System.Net"/>
    </listeners>
  </source>
</sources>
<switches>
  <add name="System.Net" value="Verbose"/>
  <add name="System.Net.Cache" value="Verbose"/>
  <add name="System.Net.Http" value="Verbose"/>
  <add name="System.Net.Sockets" value="Verbose"/>
  <add name="System.Net.WebSockets" value="Verbose"/>
</switches>
<sharedListeners>
  <add name="System.Net" type="System.Diagnostics.TextWriterTraceListener" initializeData="network.log"/>
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>

Can I achieve the same effect in .NET Core console app? In particular, I need to see which WinAPI functions are called (and which parameters are passed) during NTLM authentication with NegotiateStream class.

What I found so far is mostly about logging Web APIs in ASP.NET Core apps. My app is not ASP.NET, neither it uses any web APIs.

对于新的 .NET 5,您可以尝试这里提到的技术,它使用 EventListener。

In .NET Core tracing works differently compared to .NET framework. The framework emits trace events through etw (in windows). To read trace events you have two choices:

  • use an external tool that can parse etw logs (dotnet-trace, perfmon, perfview, an app you wrote using the traceevent library )
  • in code, by registering a custom EventListener class. Eg see this excellent article from redhat.com: link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM