Logging to windows event using Log4cxx

1.6k views Asked by At

How can i send log messages to windows event log using Log4cxx?

If i do it from multiple process , will it be process safe?

Well: Thanks Retired Ninja...Yes it works...[ log4j.properties file]

# Set root logger level to DEBUG and its only appender to EVENTLOG.
log4j.rootLogger=DEBUG, EVENTLOG

# EVENTLOG.is set to be a NTEventLogAppender

log4j.appender.EVENTLOG=org.apache.log4j.net.NTEventLogAppender
log4j.appender.EVENTLOG.server=127.0.0.1
log4j.appender.EVENTLOG.source=SomeApp

# EVENTLOG uses PatternLayout.
log4j.appender.EVENTLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.EVENTLOG.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

and simply using in the code

 #include "stdafx.h"
#include <windows.h>
#include <log4cxx/logger.h>
#include "log4cxx/propertyconfigurator.h"

using namespace log4cxx;

LoggerPtr logger(Logger::getLogger( "main"));

int  main()
{

    PropertyConfigurator::configure("log4j.properties");


    LOG4CXX_ERROR(logger, "Oh come on be serious");

    system("PAUSE");

    return 0;
}
2

There are 2 answers

2
Retired Ninja On BEST ANSWER

I was able to do it with log4cxx version 0.10.0 a couple of years ago. I haven't used log4cxx recently, so I apologize if it has changed.

Here's the config I was using:

# EVENTLOG
log4j.appender.EVENTLOG=org.apache.log4j.net.NTEventLogAppender
log4j.appender.EVENTLOG.server=127.0.0.1
log4j.appender.EVENTLOG.source=SomeApp
log4j.appender.EVENTLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.EVENTLOG.layout.ConversionPattern=[%c] %-5p: %m
log4j.appender.EVENTLOG.Threshold=ERROR
0
Some programmer dude On

It seems log4cxx doesn't have a Windows event log backend, you you will have to write one yourself. Read e.g. this for more information about the Windows event log.

The event log have to be process-safe for pretty obvious reasons: There can be a lot of processes writing to it simultaneously.