Creating a custom trace listener in C++/CLI

Topics: Building and extending application blocks, Logging Application Block
Dec 21, 2010 at 12:38 PM

I am trying to implement a custom trace listener for the Logging block for my application. I have tried to follow the instructions on MSDN as well as the hands-on tutorial without success.  Whe I try to add the Custom Trace listener using the Enterprise library configuration tool it does not show me the new custom trace listener like it is supposed to. Can anyone spot what is wrong? I have afeeling it is the class attribute in the first line.

Here is the code:

		public ref class ConsoleTraceListener abstract: public CustomTraceListener 
			virtual void TraceData(System::Diagnostics::TraceEventCache ^eventCache, String ^source, System::Diagnostics::TraceEventType eventType, int id, Object^ data) override
				if ( this->Formatter != nullptr) 

			virtual void Write(String ^message) override
			  virtual void WriteLine(String ^message) override
				// Delimit each message 
				// Write formatted message 
Dec 22, 2010 at 12:34 AM

I'm not familiar with c++ syntax but I tried copying and pasting your code to .cpp file and got compilation errors due to missing include statements.  Make sure you have the following include statements:

using namespace Microsoft::Practices::EnterpriseLibrary::Logging;
using namespace Microsoft::Practices::EnterpriseLibrary::Logging::Configuration;
using namespace Microsoft::Practices::EnterpriseLibrary::Logging::TraceListeners;
using namespace Microsoft::Practices::EnterpriseLibrary::Common::Configuration; 


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Dec 22, 2010 at 6:05 AM

Thanks , but I had included the relevant namespaces and did not have any problem compiling.

I did have a problem with the

public ConsoleTraceListener() : base()

line which I eliminated by removing the base  constructor.


My initial guess was that it might be the :

[ConfigurationElementTypeAttribute(CustomTraceListenerData::typeid)] line .

I also tried :


but with no success.

I have even tried adding the custom trace listener to the app.config file by direclty editing it but it gives an error during execution. It fails to instantiate the logger.



Dec 22, 2010 at 7:49 AM

When creating a "custom" provider (using the Custom<Block>Data configuration element) your provider must have a constructor that takes a System.Collections.Specialized.NameValueCollection parameter. Without this it doesn't work. The name/value pairs from the configuration element get passed to you via this constructor.

Your original code looked fine otherwise, just add the constructor and you should be good.