ArtinSoft's Blogs

Software Migration Experts
Welcome to ArtinSoft's Blogs Sign in | Join | Help
in Search

Mauricio Rojas Blog

Log4NET for C++

Recently my friend Yoel had just a wonderful idea. We have an old Win32 C++ application, and we wanted to add a serious logging infraestructure so we can provide better support in case the application crashes.

So Yoel came with the idea of using an existing framework for logging: LOG4NET

The only problem was, how can we integrate these two together. One way was problably exporting a .NET object as COM. But Yoel had a better idea.

Create a C++ Managed application that will comunicate with the LOG4NET assemblies, and export functions so the native applications can use that. How great is that.

Well he finally made it, and this is the code of how he did it.

First he created a C++ CLR Empty project and set its output type to Library. In the references we add a refrence to the Log4Net Library. We add a .cpp code file and we call it Bridge.cpp. Here is the code for it:

#include <atlstr.h>

using namespace System;

/// <summary>

/// Example of how to simply configure and use log4net

///
</summary>

ref class LoggingExample
{
private:
// Create a logger for use in this class
static log4net::ILog^ log = log4net::LogManager::GetLogger("LoggingExample");static LoggingExample()
{
    log4net::Config::BasicConfigurator::Configure();
}

public:static void ReportMessageWarning(char* msg)
{
String^ data = gcnew String(msg);
log->Warn(data);
}

static void ReportMessageError(char* msg)
{
String^ data = gcnew String(msg);
log->Error(data);
}

static void ReportMessageInfo(char* msg)
{
String^ data = gcnew String(msg);
log->Info(data);
}
static void ReportMessageDebug(char* msg)
{
String^ data = gcnew String(msg);
log->Debug(data);
}

};

extern "C"
{

_declspec(dllexport) void ReportMessageWarning(char* msg)
 {
LoggingExample::ReportMessageWarning(msg);
}

_declspec(dllexport) void ReportMessageError(char* msg)
{
LoggingExample::ReportMessageError(msg);
}

_declspec(dllexport) void ReportMessageInfo(char* msg)
{
LoggingExample::ReportMessageInfo(msg);
}

_declspec(dllexport) void ReportMessageDebug(char* msg)
{
LoggingExample::ReportMessageDebug(msg);
}

}

Ok. That's all. Now we have a managed C++ DLL that exposes some functions as an standard C++ DLL and we can use it with native win32 applications.

Let's do a test.

Lets create a Win32 Console application. And add this code:

// Log4NetForC++.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <atlstr.h>
extern "C"
{

_declspec(dllimport) void ReportMessageWarning(char* msg);

_declspec(dllimport) void ReportMessageError(char* msg);_declspec(dllimport) void ReportMessageInfo(char* msg); _declspec(dllimport) void ReportMessageDebug(char* msg);

}

int _tmain(int argc, _TCHAR* argv[])
{
ReportMessageWarning(
"hello for Log4NET");
ReportMessageError(
"hello for Log4NET");
ReportMessageInfo("hello for Log4NET");
ReportMessageDebug(
"hello for Log4NET");
return 0;
}

Ok. Now we just test it and we get something like:

Output

 Cool ins't it :)

Comments

 

Rongping Yan said:

Hi,  Mauricio,

 You got very smart approach to use log4net in C++. I have same project in VC++ 6.0 platform. Can you tell me how do you include the Bridge DLL into old C++ project. Thanks in advance. My email address is rongping@yahoo.com.

Rongping

June 20, 2008 2:16 PM
 

Mrojas said:

Hi Rongping. What I did is simple. You must put the Bridge.cpp into a manage dll. When you compile the DLL it will generate a .lib file. You should add that .lib file into your C++ import libraries.

To find that go to your win32 project, right click, then properties, go to Configuration Properties\  C\C++  \ Linker \ Additional dependencies

There you can add your .lib file.

The next thing you must do is put declaration like the following in a header file:

extern "C"

{

_declspec(dllimport) void ReportMessageWarning(char* msg);

_declspec(dllimport) void ReportMessageError(char* msg);

_declspec(dllimport) void ReportMessageInfo(char* msg);

_declspec(dllimport) void ReportMessageDebug(char* msg);

}

Besides that just compile and enjoy.

June 20, 2008 3:15 PM
 

Maggie said:

Did you look into Log4Cxx

logging.apache.org/.../index.html

July 14, 2008 10:04 AM
 

Mrojas said:

No not really.

The thing is that most of my C++ code is mostly legacy and I have a lot of C# and Vb.NET code, so what I really wanted was to integrate all the application logging.

Rewriting all the c++ code was too much effort, and this code works very good.

But I'll look into it.

July 14, 2008 10:31 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Non-Commercial Edition), by Telligent Systems