Setting up debugging in .Net 5

With the debug setting running in web.config, which option turns debugging on and off and what is equivalent (if any) to the next in .Net 5 (MVC 6 project)?

#define DEBUG // ... #if DEBUG Console.WriteLine("Debug version"); #endif 
+6
source share
2 answers

UPDATE

After writing this answer, I found out that the new way in .Net Core is to use environment variables. You can find the article here and more details here .

You can set the environment variable in the project properties in the debug section. The code will look after using DI to enter IHostingEnvironment

 if (env.IsDevelopment()) { //... } 

END UPDATE

The answer @ user2095880 is valid and works. However, you may need a solution that you do not need to change project.json to go to production.

 #if DEBUG app.Run(async (context) => { await context.Response.WriteAsync("Hello DEBUG CODE!"); }); #else app.Run(async (context) => { await context.Response.WriteAsync("Hello LIVE CODE!"); }); #endif 

This checks the configuration of your solution (still works in .Net 5) if you are in Debug or something else. If your solution configuration is set to Debug, the first set of code will run. If you select Release (or anything else), the second section of code will be launched. See the image below for a drop-down list to go from Debugging to Release.

enter image description here

+4
source

In the json project file you need to add:

 "frameworks": { "aspnet50": { "compilationOptions": { "define": [ "WHATEVER_YOU_WANT_TO_CALL_IT" ] } }, "aspnetcore50": { "compilationOptions": { "define": [ "WHATEVER_YOU_WANT_TO_CALL_IT" ] } } 

then in your code you use it like this:

 #if WHATEVER_YOU_WANT_TO_CALL_IT .. your code.. #endif 

where WHATEVER_YOU_WANT_TO_CALL_IT may = DEBUG or something else.

+7
source

Source: https://habr.com/ru/post/982346/


All Articles