Dev*_*inB 17 .net c# breakpoints visual-studio
无论可能实现相同结果的其他选项(即手动添加断点),是否可以以编程方式将断点添加到Visual Studio项目的源代码中?
如:
try
{
FunctionThatThrowsErrors(obj InscrutableParameters);
}
catch(Exception ex)
{
Log.LogTheError(ex);
AddBreakPointToCallingFunction();
}
Run Code Online (Sandbox Code Playgroud)
这样,当您下次运行调试时,它将自动在上次运行中导致故障的所有点上设置断点.
我不是说这是一种特别有用的调试方式.我只是想知道能力是否存在.
Joe*_*orn 45
你可以打电话System.Diagnostics.Debugger.Break()
.
您还可以告诉Visual Studio打破所有异常,甚至是处理过的异常,方法是进入菜单Debug->Exceptions...
并检查Thrown
当前只检查过"User-unhandled"的所有异常.
Dav*_*ope 39
你激励我用这个来激励我 - 感谢你让我整夜保持清醒.:)这是你可以做到的一种方式.
Visual Studio有很好的断点支持.其中一个较酷的功能是,您可以告诉它在遇到断点时运行Visual Studio宏.这些宏可以完全访问开发环境,即他们可以在键盘上手动执行任何操作,包括设置其他断点.
这个解决方案是1)在程序中放置一个顶级的try/catch来捕获所有异常,2)在运行宏的catch块中放置一个断点,以及3)让宏查看异常以找出它来自,并在那里设置断点.当您在调试器中运行它并发生异常时,您将在有问题的代码行中有一个新的断点.
拿这个示例程序:
using System;
namespace ExceptionCallstack
{
class Program
{
static void Main(string[] args)
{
try
{
func1();
}
catch (Exception e)
{
Console.WriteLine("Oops");
Console.ReadKey();
}
}
static void func1()
{
func2();
}
static void func2()
{
func3();
}
static void func3()
{
throw new Exception("Boom!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
目标是throw
在调试器中运行它时以编程方式在func3中设置断点并获取错误.为此,首先创建一个新的Visual Studio宏(我称之为我的SetBreakpointOnException).将其粘贴到新模块MyDebuggerMacros或其他任何内容中:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Public Module DebuggerMacros
Sub SetBreakpointOnException()
Dim output As String = ""
Dim stackTrace As String = DTE.Debugger.GetExpression("e.StackTrace").Value
stackTrace = stackTrace.Trim(New Char() {""""c})
Dim stackFrames As String() = Regex.Split(stackTrace, "\\r\\n")
Dim r As New Regex("^\s+at .* in (?<file>.+):line (?<line>\d+)$", RegexOptions.Multiline)
Dim match As Match = r.Match(stackFrames(0))
Dim file As String = match.Groups("file").Value
Dim line As Integer = Integer.Parse(match.Groups("line").Value)
DTE.Debugger.Breakpoints.Add("", file, line)
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)
一旦这个宏到位,返回到catch
块并使用F9设置断点.然后右键单击红色断点圆并选择"When Hit ...".在结果对话框的底部有一个选项,告诉它运行一个宏 - 下拉列表并选择你的宏.现在,当您的应用程序抛出未处理的异常时,您应该获得新的断点.
关于此的注释和警告:
希望这可以帮助!
归档时间: |
|
查看次数: |
9194 次 |
最近记录: |