使用 Log4cxx 记录到 Windows 事件
如何使用 Log4cxx 将日志消息发送到 Windows 事件日志?
如果我从多个进程执行此操作,进程安全吗?
嗯:谢谢退休的忍者...是的,它有效...[ log4j.properties 文件]
# 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
并且只需在代码中使用
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几年前我可以使用 log4cxx 版本 0.10.0 来做到这一点。我最近没有使用 log4cxx,所以如果它发生了变化,我深表歉意。
这是我使用的配置:
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:
看来 log4cxx 没有 Windows 事件日志后端,你必须自己编写一个。阅读例如此有关 Windows 事件日志的更多信息。
出于非常明显的原因,事件日志必须是进程安全的:可能有很多进程同时写入事件日志。
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.