是否有一个 C# 类用于跟踪服务方法调用(如堆栈跟踪)

发布于 2024-12-19 04:20:02 字数 337 浏览 0 评论 0原文

我知道当然有一个堆栈跟踪用于跟踪程序中对方法的调用,但是它不会显示对服务中方法的调用作为跟踪的一部分。 .NET 中是否有任何内置功能可以识别何时调用 WCF 服务,然后存储被调用方法的名称?

例如,我们有下面的方法:

private void DoStuff()
{
    object  =  Service.ServiceMethodCall();
}

在我看到的堆栈跟踪中,我可以恢复 DoStuff() 的名称和其他详细信息,但不能恢复 ServiceMethodCall()。我不想简单地将此方法的名称硬编码到我的输出中。必须有更好的方法。

谢谢

I know that there is of course a stack trace for keeping track of calls to methods within your program however it does not show a call to a method in a service as part of the trace. Is there any built in functionality in .NET to recognise when a call is made to a WCF service and then store the name of the called method?

For example we have the method below:

private void DoStuff()
{
    object  =  Service.ServiceMethodCall();
}

On the stack trace that I see I can recover the name of DoStuff() and other details but not the ServiceMethodCall(). I don't want to simply hard code the name of this method into my output. There has to be a better way.

Thanks

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

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

发布评论

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

评论(2

相思碎 2024-12-26 04:20:03

如果您尝试诊断 WCF 中的问题,可以使用内置的 WCF 跟踪功能,我们发现该功能对此不可或缺。为此(假设 WCF 托管在 IIS 中),请将以下内容添加到 web.config:

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData="c:\log\WebTrace.svclog"  />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

如果您正在调试并拥有 VS Ultimate 2010,则还可以使用 IntelliTrace

最后,如果您只是尝试跟踪发生的异常的来源,则转储异常中的 StackTrace 将显示错误的来源,假设 WCF 服务调用是链中包含异常处理程序的唯一方法。我们的开发实践要求,除非有特殊的、记录在案的情况,否则只有最外层的调用(即 WCF 服务条目)才会包含异常处理程序。这大大减少了诊断和修复错误所需的时间。

更新

看来System. Runtime.Reflection.GetCurrentMethod 可能会提供您正在查找的信息。

If you are trying to diagnose problems in WCF, you can use the built-in WCF tracing functionality, which we have found to be indispensable for this. To do this (assuming WCF hosted in IIS), add the following to web.config:

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData="c:\log\WebTrace.svclog"  />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

If you are debugging and have VS Ultimate 2010, you could also use IntelliTrace.

Finally, if you are just trying to trace the source of exceptions that occur, dumping the StackTrace in the exception will show you the source of the error, assuming that the WCF service call is the only method in the chain that contains an exception handler. Our development practices mandate that, unless there are extraordinary, documented circumstances, only the outermost call (i.e. the WCF service entry) will contain an exception handler. This provides a significant reduction in the amount of time required to diagnose and fix bugs.

Update

It appears that System.Runtime.Reflection.GetCurrentMethod may provide the information you are looking for.

抽个烟儿 2024-12-26 04:20:03

听起来您的日志记录机制根据堆栈跟踪记录您当前的方法。堆栈跟踪告诉您代码在执行完当前方法后下一步将去往何处,而不是您到目前为止已调用的方法。此外,这可能不完全准确,因为 JIT 可能会内联事物或可能发生额外的优化。

WCF 具有内置的跟踪选项,但这对于您来说可能有点过分了正在要求。否则,您将需要自己显式指定方法名称。这实际上取决于您的日志记录机制如何工作以实现最佳实现。至少,您可以仅使用字符串进行跟踪,或者在服务调用周围设置一个包装器来为您跟踪名称。

Trace.WriteLine("ServiceMethodCall");

It sounds like your logging mechanism logs your current method based on the stack trace. The stack trace tells you where your code is going next after it finishes executing the current method, not which method calls you have made so far. Additionally, this may not be entirely accurate because the JIT may inline things or additional optimizations may occur.

WCF has tracing options built in, but that may be overkill for what you're asking for. Otherwise, you will need to explicitly specify the method name yourself. It really depends on how your logging mechanism works as to the best implementation. At very least, you could just trace with a string or set up a wrapper around the service calls that traces the name for you.

Trace.WriteLine("ServiceMethodCall");

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