Entlib 5.0 Logging in C++/CLI

Topics: Logging Application Block
Oct 5, 2010 at 7:08 AM

Has anyone here tried to implement Emterprise Library 5.0 Logging in C++/CLI? I have been trying and am totally lost. There are no tutorials available so I have been looking at C# tutorials and using it to implement the same in C++/CLI but I can't seem to get it right.

Oct 5, 2010 at 7:37 AM

Are you having problems with the syntax and compile errors?  If this is the case, I may have to try using visual c++ first because I haven't yet. 

Or are you getting a runtime exception?


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Oct 5, 2010 at 9:27 AM

Thanks. I will try to explain what I am doing so that you can try to figure what I was doing wrong. I have tried to work according to the instructions in the Hands on Tutorial for Enterprise Logging Application block. The exception of course being that the code should be in C++/CLI instead of C#.

So let me retrace the steps:

  1.  New->CLR Console Application
  2. Created a new project named EL Logger

The basic code for the class is this: 

public ref class EL_Logger
	void Write(String^ message)

int main(array<System::String ^> ^args)
	EL_Logger ^logger = gcnew EL_Logger();
	logger->Write(L"Here's the Message");
    return 0;

The idea is to code the EL_logger->Write() method to call the Logging App Block. Here’s how I went about it.

  1. Project-> References-> Add New Reference ..
  2. Added the following libraries:

a. Microsoft.Practices.EnterpriseLibrary.Logging.dll
b. Microsoft.Practices.EnterpriseLibrary.Common.dll
c. Microsoft.Practices.ServiceLocation.dll

  1. Right click -> Properties -> Configuration Properties -> C/C++-> General. Change the value of “resolve #using references” to include “C:\Program Files\Microsoft Enterprise Library 5.0\Bin”
  2. Properties -> Configuration Properties -> Linker->Additional Library Directories. Add “C:\Program Files\Microsoft Enterprise Library 5.0\Bin”
  3. Add the following code to EL Logger.cpp
using namespace Microsoft::Practices::EnterpriseLibrary::Logging; using namespace Microsoft::Practices::EnterpriseLibrary::Common::Configuration; 
  1. Now I have to translate the follwing step from C# to C++(step 3 in Logging Instructions Cs.pdf)
private LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>(); 
So I created a private member:


      LogWriter ^writer;

  1. Next I create a constructor and add the line
writer = EnterpriseLibraryContainer::Current::get();

That is the part where I am stuck. Shouldn’t I get “.Current.GetInstance” here too? Can’t figure what I am doing wrong.

9) As an added measure I have also added a reference to System.Configuration and the line

using namespace System::Configuration;

10) Now coming to the configuration file:

Right Click on “Resource Files”, Add Item_> Configuration File (app.config)

I have created the configuration file as per instructions in the doc.

After compiling this is what I get:

error C2440: '=' : cannot convert from 'Microsoft::Practices::ServiceLocation::IServiceLocator ^' to 'Microsoft::Practices::EnterpriseLibrary::Logging::LogWriter ^'    d:\My Docs\Visual Studio 2008\Projects\EL Logger\EL Logger\EL Logger.cpp     19

 I know I am not doing something right. Just cant figure out what.

Oct 5, 2010 at 9:27 AM

Should I zip the sln and send it to you if it can help?

Oct 5, 2010 at 9:32 AM
Edited Oct 5, 2010 at 9:36 AM

Have you tried asking this over C++ forums?  You might be able to get faster answers there since this looks more of syntax problems.  But yes, if you could send the zip file, I wouldn't mind looking into it , might learn something new.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Oct 5, 2010 at 9:57 AM
Edited Oct 5, 2010 at 10:04 AM

This code works:

LogWriter^ a = EnterpriseLibraryContainer::Current->GetInstance<LogWriter^>();


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Oct 5, 2010 at 10:14 AM

Wow! That was real fast! Thanks! It was a syntax problem after all.

Feb 16, 2011 at 2:32 PM
Edited Feb 16, 2011 at 2:35 PM


I was dealing with a problem using the logging component in my c++ application and got stuck somewhere near the actions above. I get a runtime error when I call

LogWriter^ a = EnterpriseLibraryContainer::Current->GetInstance<LogWriter^>();

It's an exception of the following content:


An unhandled exception of type 'Microsoft.Practices.ServiceLocation.ActivationException' occurred in Microsoft.Practices.ServiceLocation.dll

Additional information: Activation error occured while trying to get instance of type LogWriter, key ""


I am really stuck :( Similar c# code works wonderfully but this one doesn't work at all. What may be the root cause of the problem?


PS here's the stack trace just in case

Microsoft.Practices.ServiceLocation.dll!Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(System.Type serviceType = {Name = "LogWriter" FullName = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter"}, string key = null) Line 57 C#
Microsoft.Practices.ServiceLocation.dll!Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance<Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter>() Line 90 + 0x3a bytes C#
clr code.exe!main(array<System::String^> ^ args = {Length=0}) Line 20 + 0x24 bytes C++
clr code.exe!mainCRTStartupStrArray(array<System::String^> ^ arguments = {Length=0}) Line 309 + 0x9 bytes C++

Thanks in advance.


Feb 16, 2011 at 2:42 PM

I used to get the error if the app.config file was not configured correctly. Check if your application is configured and the EntLib logger settings are correct.

Feb 16, 2011 at 3:01 PM

What exactly was configured incorrectly in your case? The point is that I tried to use the same app.config file for both c# project (which works fine) and for clr c++ project which fails to work.

Feb 17, 2011 at 6:45 AM

This ussually happens when the entlib configuration cannot be read by the App. You might want to check if the configuration file is in proper location. For C# Winforms or Console App the entlib configuration should be placed in the App.config to be able to be read. I'm just not sure how it works with Visual C++ since I haven't work with C++ project.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.