StackTrace 中的独特价值?

发布于 2024-08-11 08:43:16 字数 484 浏览 5 评论 0原文

背景: 我在 .NET 3.5 中编写了一个通用日志库。基本上,开发人员需要在方法开始时调用 Begin() 方法,在方法结束时调用 End() 方法。这些方法不带参数 - 库使用堆栈跟踪来确定它来自哪里。该库有一个集合,可以跟踪调用堆栈并写出每个方法的运行时间。

这效果非常好,我对此很满意。

但现在我们想将它添加到服务器。当系统上有多个用户时,只有一个日志文件,并且堆栈跟踪集中在一起。不可能知道哪个线程在做什么。

我的问题是:

有没有办法从 StackTrace 类或单独的 StackFrame 中检索唯一值?使用反射怎么样?我希望能够为每个用户创建一个单独的文件。至少,我希望能够使用唯一值标记每一行,以便我们在查看跟踪时可以按该值过滤文件。

我们正在使用 WCF TcpBinding 作为我们的服务器端通信协议(如果有帮助的话)。我正在寻找线程 ID、哈希码、地址以及一些用于区分调用堆栈来自何处的东西。

有什么想法吗?

谢谢。

Background:
I have written a generic logging library in .NET 3.5. Basically the developer needs to call the Begin() method at the beginning of their method and the End() method at the end of it. These methods do not take a parameter - the library uses the stacktrace to figure out where it came from. The library has a collection that keeps track of the call stack and writes out the elapsed time of each method.

This works really well and I'm happy with it.

But now we want to add it to the server. When multiple users are on the system, there is only one log file and the stack traces are lumped together. It's impossible to tell which thread is doing what.

My question is this:

Is there a way to retrieve a unique value from the StackTrace class or an indivdual StackFrame? What about using reflection? I would like to be able to create a seperate file for each user. At the very least, I'd like to be able to tag each line with the unique value so we can filter the file by this value when reviewing traces.

We are using WCF TcpBinding as our server side communication protocol, if that helps. I am looking for a thread id, hashcode, address, something to distinguish where the call stack came from.

Any ideas?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖正好 2024-08-18 08:43:16

您可以使用与当前线程关联的内容 - 也许是 线程 ID

线程池中的线程会被重用,因此您会看到 ID 在整个日志文件中重复出现,但在开始/结束对的生命周期内,它将唯一地标记单个用户。

You could use something associated with the current thread - perhaps the thread id?.

Threads from the thread pool get reused, so you would see the id's repeated throughout the log file, but for the lifetime of a Begin/End pair it would uniquely tag a single user.

隔纱相望 2024-08-18 08:43:16

如果您使用某种形式的面向方面编程(例如 Postsharp),您可能会找到更好的声明性方式来获取所需的信息。 Thread.CurrentThread.ManagedThreadId 将为您提供当时运行代码的线程的引用,但您的开发人员所要做的就是将属性应用于方法,而不是调用 每个方法的 Begin()End()

If you used some form of Aspect Oriented Programming (like Postsharp) you might find a better, declarative way to get the information you need. Thread.CurrentThread.ManagedThreadId would give you a reference for the thread running the code at the time, but all your developers would have to do do is apply an attribute to a method, rather than calling Begin() and End() for every method.

游魂 2024-08-18 08:43:16

要获取当前线程运行所在的用户帐户,可以使用WindowsIdentity.GetCurrent()

To get the user account under which the current thread is running, you can use WindowsIdentity.GetCurrent().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文