We inherited a somewhat poorly-designed WCF service that we want to improve. One problem with it is that it has over a hundred methods (on two different interfaces), most of which we suspect are not used. We decided to put some logging on each of the methods to track when and how they're called. To make the tracing code refactor-friendly and typo-proof, we implemented it like so:
public void LogUsage()
{
try
{
MethodBase callingMethod = new StackTrace().GetFrame(1).GetMethod();
string interfaceName = callingMethod.DeclaringType.GetInterfaces()[0].Name;
_loggingDao.LogUsage(interfaceName, callingMethod.Name, GetClientAddress(), GetCallingUrl());
}
catch (Exception exception)
{
_legacyLogger.Error("Error in usage tracking", exception);
}
}
LogUsage()
is then called at the start of each method we want to trace.
The service is very high traffic, on the order of 500,000+ calls/day. 99.95% of the time, this code executes beautifully. But the other 0.05% of the time, GetInterfaces()
returns an empty (but not null
) array.
Why would GetInterfaces()
occasionally return inconsistent results?
This may seem so trivial - a 0.05% error rate is something we can usually only dream of. But the whole point is to identify all the service touchpoints, and if this error is always coming out of one (or a few) method calls, then our tracing is incomplete. I've tried to reproduce this error in my development environment by calling each and every method on the service, but to no avail.
callingMethod
, you might find that its declaring type actually has no interfaces.LogUsage
is only called from methods inside two classes, and both those classes implement at least one interface.