如何在生产环境中记录C#应用程序的并行堆栈信息

huangapple go评论89阅读模式
英文:

How to log Parallel Stack info in a C# application in production

问题

以下是您要翻译的内容:

"Is there any way to log or print the information of Visual Studio parallel stack window?

I have a c# multithreading application running in production that is experiencing an issue without leaving any clue. It seems to be a deadlock of something like that but I cannot replicate it in a development environment.

So I think it would be very helpful if I can log to a file the status of the app (the running threads and their call stack) when I detect programmatically that the issue is happening.

Thanks in advance

Edit: Thanks to the answers and after investigating I have found that the best option is to generate a memory dump and load it in Visual Studio. However, I have a new problem... I am using createdump for doing that and it works on a .Net process, but the thing is that the process we have in production is a java process that uses a .Net library with JNI, so createdump is not working for that process. Any ideas about how to handle that?

The error I am receiving is: open(/proc/69588/mem) FAILED 13 (Permission denied)"

英文:

Is there any way to log or print the information of Visual Studio parallel stack window?

I have a c# multithreading application running in production that is experiencing an issue without leaving any clue. It seems to be a deadlock of something like that but I cannot replicate it in a development environment.

So I think it would be very helpful if I can log to a file the status of the app (the running threads and their call stack) when I detect programmatically that the issue is happening.

Thanks in advance

Edit: Thanks to the answers and after investigating I have found that the best option is to generate a memory dump and load it in Visual Studio. However, I have a new problem... I am using createdump for doing that and it works on a .Net process, but the thing is that the process we have in production is a java process that uses a .Net library with JNI, so createdump is not working for that process. Any ideas about how to handle that?

The error I am receiving is: open(/proc/69588/mem) FAILED 13 (Permission denied)

答案1

得分: 2

如果问题是死锁,您应该能够获取内存转储。然后,您应该能够在Visual Studio中打开它并调查程序状态。还有一些便携式调试器,比如dnSpy,可以用于附加到运行中的程序并调查状态。

记录调用堆栈可能不是一个好主意,因为它会很慢,您无法捕获特定时刻的快照,因为至少需要一个线程来运行捕获。如果坚持要这样做,可以查看在C#中获取所有线程的堆栈跟踪

但我建议在调试困难问题时使用一般方法。找到一种重现问题的方式,这样您就有办法知道您已经解决了它。这可能需要对您的测试环境进行更改,以更好地匹配生产环境。

审查代码,如果是死锁问题,它与锁定有关。您应该(理想情况下)只保持锁定很短的时间,而且您应该能够准确地看到在保持锁定期间将运行的代码。任何在持有锁定的同时可能阻塞的代码都是可疑的,而任何运行“未知”代码,比如事件处理程序,都有可能阻塞。

改进日志记录,以便您更深入地了解系统正在做什么。使用多个日志级别和模块,这样您可以在不被大量日志消息淹没的情况下缩小问题范围。

英文:

If the issue is a deadlock you should be able to take a memory dump. You should then be able to open this in Visual studio and investigate the program state. There are also portable debuggers, like dnSpy that could be used to attach to a running program and investigate the state.

Logging of callstacks is likely not good idea, since it will be slow you cannot capture a snapshot of any specific moment in time, since at least one thread need to run to do the capturing. If you insist you could look at get the stacktraces for all threads in c#.

But I would suggest using the general approach when debugging difficult issues. Find some way to reproduce the issue, so you have some way to know that you have fixed it. This might require changes to your testing environment to better match production.

Review the code, if it is a deadlock it has to do with locking. You should (ideally) only hold locks for a very short time, and you should be able to see exactly what code will run while holding the lock. Any code that could block while holding the lock is suspect, and anything running "unknown" code, like an event handler, could potentially block.

Improve the logging so you have more insight into what the system is doing. Use multiple logging levels and modules so you can narrow down the problem without being overwhelmed by logging messages.

huangapple
  • 本文由 发表于 2023年7月20日 17:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定