Requirements:
VS Studio C++ 2010 Express edition and the source code can be obtained from log4cplus at sourceforge.
Unpack the log4cplus sources. Open the log4cplus.sln solution file located in the msvc8 folder. The solution explorer should contain the log4cplus_dll project.
The default target is set to DEBUG mode. Make sure to alter the target to DEBUG_UNICODE!
Open the context menu for this entry and hit compile.
The log4cplusUD.lib and log4cplusUD.dll should be created.
1> Bibliothek ".\.\Win32\log4cplus_dll.Debug_Unicode\log4cplusUD.lib" und Objekt ".\.\Win32\log4cplus_dll.Debug_Unicode\log4cplusUD.exp" werden erstellt. 1> log4cplus_dll.vcxproj -> *\log4cplus-1.0.4\msvc8\.\Win32\log4cplus_dll.Debug_Unicode\log4cplus_dll.dll ========== Alles neu erstellen: 1 erfolgreich, Fehler bei 0, 0 übersprungen ==========
Create some folders (e.g. bin, lib, and include) at a specific location and place the binary, the library and the header files into it.
Add the bin folder (e.g. C:\Users\Public\bin) to the path environment variable.
VS 2010 integration:
Create a new console application using VS 2010. Open the project properties and add the additional include folder. Make sure you are targeting the DEBUG mode.
Add the additional lib folder and add the required log4cplusUD.lib as follows.
Try to compile without any erros. Make sure if you target DEBUG mode that you used lib4cplusUD.lib not the lib4cplusD.lib or the lib4cplus.lib!
Modify the working directory for the debugger to the $TargetDir. Otherwise you have to fiddle the path to the properties file on your own. Take a look at the #ifdef DEBUG pragma of the main.cpp underneath.
Create a simple property file (e.g. named log.properties) for the application and place this file into the output folder where your application gets compiled to (e.g. *\Debug).
Make sure you have the privileges to create a file in the location – and this location exists – defined by the property file.
log4cplus.rootLogger=INFO, STDOUT, FILEAPPENDER #log4cplus.logger.main=INFO #log4cplus.logger.utils=FILEAPPENDER log4cplus.appender.STDOUT=log4cplus::ConsoleAppender log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout log4cplus.appender.STDOUT.layout.ConversionPattern=%d{%m/%d/%y %H:%M:%S} [%t] %-5p %c{2} %%%x%% - %m [%l]%n log4cplus.appender.FILEAPPENDER=log4cplus::RollingFileAppender log4cplus.appender.FILEAPPENDER.File=/tmp/consoleapplication.log log4cplus.appender.FILEAPPENDER.MaxFileSize=5MB #log4cplus.appender.FILEAPPENDER.MaxFileSize=500KB log4cplus.appender.FILEAPPENDER.MaxBackupIndex=1 log4cplus.appender.FILEAPPENDER.layout=log4cplus::TTCCLayout
In VS modify the main.cpp to the following.
#include "stdafx.h" #include #include #ifdef DEBUG #include #endif int _tmain(int argc, wchar_t *argv[]) { // Use the log4cplus namespace using namespace log4cplus; // Load the properties #ifdef DEBUG std::wstring applicationPath(argv[0]); std::wstringstream fullqualifedPath; fullqualifedPath << applicationPath.substr(0, applicationPath.find_last_of(L"/\\")) << L"\\" << L"log.properties"; PropertyConfigurator::doConfigure(fullqualifedPath.str()); #else PropertyConfigurator::doConfigure(L"log.properties"); #endif // Create the logger const Logger logger = Logger::getInstance("main"); // Log with INFO level if (logger.isEnabledFor(INFO_LOG_LEVEL)) { LOG4CPLUS_INFO(logger, "Application startup"); } // Log with INFO level if (logger.isEnabledFor(INFO_LOG_LEVEL)) { LOG4CPLUS_INFO(logger, "Application shutdown"); } }
Make sure you have set the build target to DEBUG not RELEASE.
Compile and run the application. If the PATH variable does not include the log4cplusUD.dll an error message should appear at startup. If the property file cannot be found, the logger will complain about not having any appenders.
log4cplus:ERROR No appenders could be found for logger (main). log4cplus:ERROR Please initialize the log4cplus system properly.
The following output should be generated on the console.
01/08/12 06:50:05 [3079005904] INFO main %% - Application startup [../Log4CplusClient/main.cpp:18] 01/08/12 06:50:05 [3079005904] INFO main %% - Application shutdown [../Log4CplusClient/main.cpp:24]
The consoleapplication.log looks like.
2090 [3592] INFO main <> - Application startup 2106 [3592] INFO main <> - Application shutdown