如何检测是否调试

如何检测是否调试

本文介绍了如何检测是否调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .Net Core 控制台应用程序.在我的 startup.cs 中的配置方法中,我试图测试是否启用了调试器:

I have a .Net Core console application. In the configure method in my startup.cs I am trying to test if the debugger is enabled or not:

 if (HttpContext.Current.IsDebuggingEnabled)
     loggerFactory.AddConsole(Configuration);
 else
     loggerFactory.AddConsoleJson(Configuration);

HttpContext.Current.IsDebuggingEnabled 在 .Net 核心中不支持.我还没有找到一种在 .net core 中有效的方法.

HttpContext.Current.IsDebuggingEnabled doesn't seam to be supported in .Net core. I havent been able to find a method that works in .net core.

System.Diagnostics.DebuggableAttribute.DebuggingModes.Default 似乎也不起作用.

推荐答案

HttpContext 是另一种 HttpContext,因为您现在使用的是 ASP.NET Core.在 ASP.NET Core 变体中找不到该属性.给出了差异的解释.

HttpContext is another HttpContext than you were used to since you are now using ASP.NET Core. That property can't be found in the ASP.NET Core variant. An explanation of the differences is given by poke.

我会使用 Debugger.IsAttached,它不仅会检查是否启用了调试,还会被主动调试.

I would use Debugger.IsAttached, which not only checks if debugging is enabled, but also actively being debugged.

这篇关于如何检测是否调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:55