简体   繁体   English

Visual Studio 调试日志(如 Android 中的 logcat)?

[英]Visual Studio debug log (like logcat in Android)?

I'm trying to debug a C#/.Net program in Visual Studio 2010 but it's timing-sensitive so breakpoints mess it up.我正在尝试在 Visual Studio 2010 中调试 C#/.Net 程序,但它对时间敏感,因此断点会将其弄乱。 So I just want to log certain events and their relative times and watch them as my program is running.所以我只想记录某些事件及其相对时间,并在我的程序运行时观察它们。

Does Visual Studio (or C# or .Net) have any facility for writing to a log file that Visual Studio can display as a debug window? Visual Studio(或 C# 或 .Net)是否有任何工具可以写入 Visual Studio 可以显示为调试窗口的日志文件?

When I do Android development in Eclipse I can take advantage of their Log class and insert lines that looks like this in my code:当我在 Eclipse 中进行 Android 开发时,我可以利用他们的 Log 类并在我的代码中插入如下所示的行:

  Log.d("Label1", "Hit checkpoint X");  

And they will appear along with time/date stamps in the LogCat window which is displayed along with other windows in Eclipse.它们将与时间/日期戳一起出现在 LogCat 窗口中,该窗口与 Eclipse 中的其他窗口一起显示。

What's the closest equivalent to this in Visual Studio 2010? Visual Studio 2010 中最接近的等价物是什么?

You can set a breakpoint that doesn't actually break, but outputs a log message instead.您可以设置一个实际上不会中断的断点,而是输出一条日志消息。 Just set a breakpoint, then right click on the breakpoint and select "When Hit...".只需设置一个断点,然后右键单击断点并选择“When Hit...”。

From that dialog select "Print a message" and "continue execution"从该对话框中选择“打印消息”和“继续执行”

对话框截图

There isn't really anything built in exactly like logcat, but there are lots of logging frameworks that you can use.没有真正像 logcat 那样内置的任何东西,但是有很多日志框架可以使用。

Personally, I like to use NLog and set up a UDP target for tracing/debugging within my configuration file along with a rule to forward all loggers to the target.就我个人而言,我喜欢使用NLog并在我的配置文件中设置一个用于跟踪/调试的 UDP 目标以及将所有记录器转发到目标的规则。 I think that NLog is easier to use than Log4Net (the .NET port of Log4j).我认为 NLog 比 Log4Net(Log4j 的 .NET 端口)更容易使用。 Once you do this you can create a logger from the manager and call the logger just like LogCat in android:完成此操作后,您可以从管理器创建一个记录器,并像 Android 中的 LogCat 一样调用记录器:

Logger logger = LogManager.GetLogger("MyClassTag");

logger.Trace("Something to log");
logger.Debug("Something to log");
logger.Info("Something to log");
logger.Warn("Something to log");
logger.Error("Something bad to log", exception);
logger.Fatal("Something bad to log", exception);

For listening to the UDP logging packets I use Log2Console which allows me to view them just like android's logcat viewer.为了收听 UDP 日志数据包,我使用Log2Console ,它允许我像 android 的 logcat 查看器一样查看它们。

For whatever reason, this question was at the top of my Google search when looking for something similar (though it's 5 years old), so in case anyone else comes across it, this is the easiest method I've encountered:无论出于何种原因,当我寻找类似的东西时(虽然它已经有 5 年历史),这个问题在我的谷歌搜索中排在首位,所以如果其他人遇到它,这是我遇到的最简单的方法:

Debug.WriteLine("hit checkpoint X");

It's included in System.Diagnostics, so you'll need a using System.Diagnostics at the top of your file, but that's all you need.它包含在 System.Diagnostics 中,因此您需要在文件顶部using System.Diagnostics ,但这就是您所需要的。

Use Android Logcat Output plugin:使用Android Logcat 输出插件:
https://marketplace.visualstudio.com/items?itemName=LancelotChen.AndroidLogcatOutput https://marketplace.visualstudio.com/items?itemName=LancelotChen.AndroidLogcatOutput

Visual Studio Tool Window Extension for Android Logcat Output & Filter用于 Android Logcat 输出和过滤器的 Visual Studio 工具窗口扩展

Inspired by NVidia Nsight Tegra.灵感来自 NVidia Nsight Tegra。

To launch the logcat output window, use Menu要启动 logcat 输出窗口,请使用 Menu

View --> Other Windows --> Logcat Output查看 --> 其他窗口 --> Logcat 输出

Then the logcat output window will show.然后将显示 logcat 输出窗口。

On the left pane is filter list, you can add new filetr. 左侧窗格是过滤器列表,您可以添加新的文件。 Make one filter checked, the logs will be filtered. 勾选一个过滤器,日志将被过滤。



Right-click on filter will bring out conext menu, which you can use to delete or edit filter settings.右键单击过滤器将显示上下文菜单,您可以使用该菜单删除或编辑过滤器设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM