Hello everybody, I have a question about logging with log4cxx from different process. I have two different .exe that link the same log4cxx dll. I would like that both the exe uses the same configuration file and so write in the same log file. I was able to get this result, however I think that I did something wrong.
I'm generating two .exe by building the attached cpp code, and both load the conf file in attachment. When I run the exes, it seems that everything works fine, and the log file example.log is fine. However I don't understand if my approach is fine. That is, how it is possible that the two processes don't access at the same time the log file generating by this way a conflict and corrupting it? I think that I should generate a logging exe that receives the messages from other processes and that then, from a single exe, I should access the log. Is there any suggestion? I'm a bit confused, any link/idea/feedback will be appreciated
#include <stdlib.h> using namespace com::foo; // include log4cxx header files. #include "log4cxx/logger.h" #include "log4cxx/basicconfigurator.h" #include "log4cxx/propertyconfigurator.h" #include "log4cxx/helpers/exception.h" using namespace log4cxx; using namespace log4cxx::helpers; // Define a static logger variable so that it references the // Logger instance named "MyApp". LoggerPtr logger(Logger::getLogger("MyApp")); int _tmain(int argc, _TCHAR* argv[]) { SYSTEMTIME localTime; GetLocalTime(&localTime); std::srand(localTime.wSecond); //use current time as seed for random generator // Variabile differente per ogni istanza del processo int processId = std::rand(); int result = EXIT_SUCCESS; try { if (1) { // Vedo solo gli errori di foo PropertyConfigurator::configure("\\Flashdisk\\log4j2.conf"); // Vedo tutto di foo //PropertyConfigurator::configure("\\Flashdisk\\log4j.conf"); } else { BasicConfigurator::configure(); } std::string testo; int counter = 0; while(1) { counter++; LOG4CXX_INFO(logger, ">>>>> " << processId << " Entering Contatore " << counter); LOG4CXX_INFO(logger, "<<<<< " << processId <<" Leaving Contatore " << counter); // Aspetto un po`, time variabile tra un processo e l'altro Sleep(localTime.wSecond*50); } } catch(Exception&) { result = EXIT_FAILURE; } return result; }
log4j2.conf
Description: Binary data