9

I'm migrating a console app from .NET Legacy to .NET Core 2.2. In that app, I'm using the HttpClient and HttpRequestMessage classes. Sometimes, requests made with these classes fail. For that reason, I had a system.diagnostics block in my App.config file to log the raw request for diagnosing issues. While this worked in my legacy app, I now get an error in .NET Core.

When I start my app, I see the following error:

ConfigurationErrorsException: Unrecognized configuration section system.diagnostics. 

The only thing I've added to my App.config file is: <system.diagnostics></system.diagnostics>, which is an empty config block. If I remove that block, my app runs as expected.

How do I add the system.diagnostics configuration used in my legacy app into my .NET Core app so I can trace the raw web requests again?

Thanks!

Some User
  • 5,257
  • 13
  • 51
  • 93
  • Are you sure that your application doesn't have a system.diagnostics block inside of the 'configuration' block already? If so you can't instantiate two blocks in the same project. – Dr. Roggia Jul 17 '19 at 14:28
  • https://stackoverflow.com/questions/55477684/how-to-configure-network-tracing-dotnet-core-for-httpclient-calls – CodeCaster Jul 17 '19 at 14:32
  • @Dr.Roggia How would I know? I have a single App.config file. It's pretty empty. Does .NET core automatically inject some configuration details for console apps that I'm not aware of? – Some User Jul 17 '19 at 14:48
  • @CodeCaster The SO question uses the XML config approach, which is what I'm using and want. However, the answer is for ASP.NET and uses the .json config syntax. I'm not using ASP.NET, I'm building a console app. I want to use the XML config approach and not the .json approach. – Some User Jul 17 '19 at 14:49
  • I think the point is that tracing did not make it into .NET Core yet. Not sure about that though, haven't needed it yet. See also https://learn.microsoft.com/en-us/aspnet/core/signalr/diagnostics?view=aspnetcore-2.2, try `{ "Logging": { "LogLevel": { "System.Net": "Trace" } } }`. – CodeCaster Jul 17 '19 at 14:56
  • 1
    https://github.com/dotnet/corefx/issues/29210 – Lex Li Jul 17 '19 at 15:49

1 Answers1

21

The thing is that .NET Core doesn't pre-register config section for system.diagnostics.

Try to emplace this at the beginning of App.config, just under <configuration> line:

<configSections>
    <section name="system.diagnostics" type="System.Diagnostics.DiagnosticsConfigurationHandler"/>
</configSections>
Pang
  • 9,564
  • 146
  • 81
  • 122
vSzemkel
  • 624
  • 5
  • 11