使用 Log4cxx 记录到 Windows 事件

发布于 2024-12-29 05:29:03 字数 1055 浏览 0 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽 2025-01-05 05:29:03

几年前我可以使用 log4cxx 版本 0.10.0 来做到这一点。我最近没有使用 log4cxx,所以如果它发生了变化,我深表歉意。

这是我使用的配置:

# 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

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
无人问我粥可暖 2025-01-05 05:29:03

看来 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文