0

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;
}
Novalis
  • 2,265
  • 6
  • 39
  • 63

2 Answers2

1

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
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
  • How can i set the different eventlog name? since this just log to general event log? I try applicationName but does not work – Novalis Jan 26 '12 at 13:33
  • @Novalis It's all in the source. Please refer this: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363661(v=vs.85).aspx – Dig Sep 29 '12 at 18:28
0

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621