如何从Java写入Windows事件日志?

时间:2008-10-02 22:20:36

标签: java windows logging event-log

如何从Java写入Windows事件日志?

4 个答案:

答案 0 :(得分:24)

Log4J是一个基于Java的日志记录实用程序。类NTEventLogAppender可用于“附加到NT事件日志系统”。请参阅此处的文档:

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/nt/NTEventLogAppender.html

修改:有一个较新的版本Log4j 2“,与其前任版本相比有了显着改进。”

答案 1 :(得分:8)

您可以使用JNA直接写入事件日志,而无需任何本机DLL。有关各种事件日志方法,请参阅Advapi32和Advapi32Util类(自JNA 3.2.8以来发布)。

如果您使用的是Log4j,请考虑Log4jna而不是NTEventLogAppender。

答案 2 :(得分:5)

早在2001年,JavaWorld发表了一篇关于如何write messages to the Windows NT Event Log.的文章或者,您可以看一下Log4j NTEventLogAppender类。

答案 3 :(得分:5)

您还可以在Windows XP Pro及更高版本上使用eventcreate命令。

String command = "eventcreate "
               + " /l APPLICATION"
               + " /so \"" + applicationObjectName + "\""
               + " /t " + lvl
               + " /id " + id
               + " /d \"" + description + "\"";

Runtime.getRuntime().exec(command);

对于XP home及更低版本,您可以创建使用wscript.shell.eventcreate方法写入的vbs应用程序。但是你牺牲了指定源的能力。

实施例: http://www.ozzu.com/mswindows-forum/posting-event-log-with-batch-files-t76791.html