Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...