Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core - Using system.diagnostics in App.config

Tags:

c#

.net-core

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!

like image 672
Some User Avatar asked Jul 17 '19 14:07

Some User


People also ask

Does .NET Core support app config?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.

Does .NET Core still use web config?

ASP.NET Core no longer uses the Global. asax and web. config files that previous versions of ASP.NET utilized.

Does .NET Core need web config?

In order to set up the ASP.NET Core Module correctly, the web. config file must be present at the content root path (typically the app base path) of the deployed app.


1 Answers

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>
like image 53
vSzemkel Avatar answered Sep 24 '22 12:09

vSzemkel