On Nov 26, 2007, at 9:08 PM, Vic Simkus wrote:
Hello
I'm trying to programmatically configure the NT event log appender and
having zero luck. Everything compiles but there are no entries in the
event viewer. Bellow is the code. What am I missing here?
log4cxx::nt::NTEventLogAppender * ntAppender = new
log4cxx::nt::NTEventLogAppender();
ntAppender->setSource(L"Source");
ntAppender->setName(L"ntlogappender");
ntAppender->setLayout(log4cxx::LayoutPtr(new
log4cxx::SimpleLayout()));
log4cxx::helpers::Pool p;
ntAppender->activateOptions(p);
log4cxx
::BasicConfigurator::configure(log4cxx::AppenderPtr(ntAppender));
log4cxx::Logger::getRootLogger()-
>setLevel(log4cxx::Level::getDebug());
logger = log4cxx::Logger::getLogger("logger");
LOG4CXX_INFO(logger,"Created NTEventLogAppender contingency
appender");
When programmatically configuring appenders, layouts, etc,
activateOptions should be called after configuration. You should have
gotten a console message "NT Event Log not opened".
Some stylistic issues:
When calling methods that take LogString with string literals, you
should enclose them in LOG4CXX_STR instead of prefixing with L., like:
ntAppender->setSource(LOG4CXX_STR("Source"));
ntAppender->setName(LOG4CXX_STR("ntlogappender"));
L is only appropriate when logchar is wchar_t (typical on Windows),
but would be inappropriate when logchar is char (typical on Unix).
Obviously, it is a bigger deal when you are using a logger that is not
platform specific.
Using raw pointers is atypical, I would have done
log4cxx::nt::NTEventLogAppenderPtr ntAppender(new
log4cxx::nt::NTEventLogAppender());