对于C#日志记录,如何获取调用堆栈深度以最小的开销? [英] For C# logging, how to obtain call stack depth with minimal overhead?

查看:112
本文介绍了对于C#日志记录,如何获取调用堆栈深度以最小的开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个log4net的包装(我可能会赞成NLOG的疲态,还没有决定),我缩进记录的消息导致给调用结构的想法。例如:

I have created a wrapper for log4net (which I may be dropping in favor of NLog, haven't decided yet), and I indent the logged messages result to give an idea of calling structure. For example:

2011-04-03 00:20:30,271 [CT] DEBUG  -     Merlinia.ProcessManager.CentralThread.ProcessAdminCommand - ProcStart - User Info Repository
2011-04-03 00:20:30,271 [CT] DEBUG  -      Merlinia.ProcessManager.CentralThread.StartOneProcess - User Info Repository
2011-04-03 00:20:30,411 [CT] DEBUG  -       Merlinia.ProcessManager.CentralThread.SetProcessStatus - Process = User Info Repository, status = ProcStarting
2011-04-03 00:20:30,411 [CT] DEBUG  -        Merlinia.ProcessManager.CentralThread.SendProcessStatusInfo
2011-04-03 00:20:30,411 [CT] DEBUG  -         Merlinia.CommonClasses.MhlAdminLayer.SendToAllAdministrators - ProcessTable
2011-04-03 00:20:30,411 [CT] DEBUG  -          Merlinia.CommonClasses.MReflection.CopyToBinary
2011-04-03 00:20:30,411 [CT] DEBUG  -           Merlinia.CommonClasses.MReflection.CopyToBinary - False
2011-04-03 00:20:30,411 [CT] DEBUG  -          Merlinia.CommonClasses.MhlBasicLayer.SendToAllConnections - 228 - True - False
2011-04-03 00:20:30,411 [CT] DEBUG  -           Merlinia.CommonClasses.MmlNonThreaded.SendObject - 228
2011-04-03 00:20:30,411 [CT] DEBUG  -            Merlinia.CommonClasses.MllTcpSocket.SendMessage - 228 - True
2011-04-03 00:20:32,174 [10] DEBUG  -    Merlinia.CommonClasses.MReflection.CreateFromBinary
2011-04-03 00:20:32,174 [10] DEBUG  -     Merlinia.CommonClasses.MReflection.CopyFromBinary - Bytes = 71
2011-04-03 00:20:32,174 [CT] DEBUG  - Merlinia.ProcessManager.CentralThread.MessagingCallback - User Info Repository - ProcessInfoAndRequests
2011-04-03 00:20:32,174 [CT] DEBUG  -  Merlinia.ProcessManager.CentralThread.ProcessProcessInfoAndRequests - User Info Repository

我做到这一点使用System.Diagnostics.StackTrace和计数StackFrames。

I do this using System.Diagnostics.StackTrace and counting StackFrames.

现在,这里的问题:是否有这样做的任何更有效的方法?我只需要确定的(相对)调用堆栈的深度,即是当前深度加上或减去什么是最后一次我记录的包装被调用。 (请注意,我不实际使用将StackFrame对象 - 我得到的方法名,否则)

Now here's the question: Is there any more efficient way of doing this? I only need to determine the (relative) call stack depth, i.e., is the current depth plus or minus what it was the last time my logging wrapper was called. (Note that I do not actually use the StackFrame objects - I get the method names otherwise.)

什么,我希望的是查询的一些简单高效的方式调用堆栈深度或堆栈使用情况。

What I'm hoping for is some simple high-performance way of querying the call stack depth or stack usage.

推荐答案

只需使用的 StackTrace.FrameCount 财产,并将其与先前记录的 FrameCount 。仅供参考, FrameCount 可能是最快的方法来检索实际的帧数,因为它只返回内部 m_iNumOfFrames 现场发回给你。

Simply use the StackTrace.FrameCount property, and compare it to the previously recorded FrameCount. FYI, FrameCount is probably the fastest method to retrieve the actual frame count, since it only returns the internal m_iNumOfFrames field back to you.

这篇关于对于C#日志记录,如何获取调用堆栈深度以最小的开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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