2

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

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
CSharpened
  • 11,674
  • 14
  • 52
  • 86
  • Take a look here: http://stackoverflow.com/questions/280413/c-sharp-how-do-you-find-the-caller-function – Samich Dec 01 '11 at 15:49
  • How are you accessing this stack trace to output it? – Dan Puzey Dec 01 '11 at 15:52
  • I am using this code to get the most recent method call. StackTrace stackTrace = new StackTrace(); _callingFunction = stackTrace.GetFrame(0).GetMethod().Name; – CSharpened Dec 01 '11 at 15:54
  • 1
    You could only see the ServiceMethodCall if you were inside of the ServiceMethod. Throw an exception from the service and see what the stack looks like. Also, please tell us what you're trying to accomplish. – John Saunders Dec 01 '11 at 17:29
  • An interceptor (e.g. a Unity `IInterceptionBehavior`) might be an option... – Kit Dec 01 '11 at 17:48
  • I am simply wanting to output the name of the service method in the application that makes the call. I thought that as the method is called from within the application it would have a reference to it somewhere which would make the name available for output. It isn't critical to what I am doing but it made me wonder whether it was possible in a similar way to how a stack trace works. – CSharpened Dec 05 '11 at 09:25
  • But it wouldn't work for a stack trace. A stack trace only contains the method when it's in the method or in a method called by it. – John Saunders Dec 05 '11 at 18:58

2 Answers2

2

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.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
0

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");

Chris Hannon
  • 4,134
  • 1
  • 21
  • 26