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; 
}


http://blogs.artinsoft.net/mrojas/archive/2008/06/19/log4net-for-c.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值