Wrapping the Tracing functionality(Tracer class) of MS-Logging application block

Topics: Logging Application Block
Jul 23, 2009 at 10:43 AM

Hi All,

I am tring to wrapp the Tracer class with MyTracerClass. Every thing works fine but it writes the stacktrace of my wrapper method,

 

I want it should write the caller method of my application which is using my wrapped class.

ex.

class MyTracer:Tracer

{

public MyTracer(string operation):base(operation)

{

}

}

 

calling method.

public void myMethod()

{

using(new MyTracer("Trace"))

{

//body

}

}

It is writing the trace as below

----------------------------------------
Timestamp: 2009-07-23 15:50:51.507 
Message: Start Trace: Activity '1847b85f-cd44-4166-8b87-0de22f8fa714' in method 'Microsoft.Practices.EnterpriseLibrary.Logging.Tracer.GetExecutingMethodName' at 82670183554 ticks

 

where as I want it should print like

----------------------------------------
Timestamp: 2009-07-23 15:50:51.507 
Message: Start Trace: Activity '1847b85f-cd44-4166-8b87-0de22f8fa714' in method 'MyNamespace.MyMethod' at 82670183554 ticks

 

Any help will be appriciated. Thanks in Advance

Jul 24, 2009 at 10:52 AM

Hi,

The problem with inheriting from Tracer class is by the time you reach the highlighted text, the declaring type (where GetExecutingMethodName is declared - Tracer) is not the same anymore as the current type this method is currently executing (MyTracer). 
It thinks that it doesn't own GetExecutingMethodName method and assumes that it has already found the executing method in the call stack.
		private string GetExecutingMethodName()
		{
			string result = "Unknown";
			StackTrace trace = new StackTrace(false);

			for (int index = 0; index < trace.FrameCount; ++index)
			{
				StackFrame frame = trace.GetFrame(index);
				MethodBase method = frame.GetMethod();
				if (method.DeclaringType != GetType())  //Microsoft.Practices.EnterpriseLibrary.Logging.Tracer != WrappingTheTracer.MyTracer
				{
					result = string.Concat(method.DeclaringType.FullName, ".", method.Name);
					break;
				}
			}

			return result;
		}
 
Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Jul 28, 2009 at 10:51 AM

Hi ,

Thanks for reply.

The trace is getting printed as that is not matching with the current declaring type. As we need to supress this entry what we can do?

Actually the stack trace is containing following entry.

1. Microsoft.EntLib.Log.Tracer

2. MyTraceClass.

3. MyCallingMethod.

 

if (method.DeclaringType != GetType())  //Microsoft.Practices.EnterpriseLibrary.Logging.Tracer != WrappingTheTracer.MyTracer


As the above statement is eliminating the first match and while checking with second match it is getting condition true. I want to eliminate the second entry as well.

The simple solution is to check inside this if that if the GetType() is not eqaual to the MyTraceClass. only then print the tracing.

But it envolves the change to be made in GetExecutingMethodName() method..


Is this recommended to make changes in the source code?


I really don't want to change the source code.

Is there any solution that resolves this problem.


Thanks in advance.
Jul 29, 2009 at 7:44 AM
Edited Jul 29, 2009 at 7:49 AM

If you don't want to change the source code, I'd suggest you just copy the entlib class and modify that copy instead.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com