如何在 .NET 中毫无例外地打印当前的 Stack Trace? [英] How to print the current Stack Trace in .NET without any exception?

查看:23
本文介绍了如何在 .NET 中毫无例外地打印当前的 Stack Trace?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个普通的 C# 代码.我没有例外.我想以编程方式记录当前堆栈跟踪以进行调试.示例:

I have a regular C# code. I have no exceptions. I want to programmatically log the current stack trace for debugging purpose. Example:

public void executeMethod() 
{
    logStackTrace();
    method();
}

推荐答案

查看 System.Diagnostics 命名空间.里面有很多好东西!

Have a look at the System.Diagnostics namespace. Lots of goodies in there!

System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace();

深入了解幕后发生的事情真是太好了.

This is really good to have a poke around in to learn what's going on under the hood.

我建议您查看日志记录解决方案(例如 NLog、log4net 或 Microsoft 模式和实践企业库),它们可能会实现您的目的,然后是一些.

I'd recommend that you have a look into logging solutions (Such as NLog, log4net or the Microsoft patterns and practices Enterprise Library) which may achieve your purposes and then some.

这篇关于如何在 .NET 中毫无例外地打印当前的 Stack Trace?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆