简体   繁体   English

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

[英]How to write from Java to the Windows Event Log?

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

Log4J is a Java-based logging utility. Log4J是一个基于Java的日志记录实用程序。 The class NTEventLogAppender can be used to "append to the NT event log system". NTEventLogAppender类可用于“附加到NT事件日志系统”。 See the documentation here: 请参阅此处的文档:

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

Edit: There is a newer version, Log4j 2 "that provides significant improvements over its predecessor." 编辑:有一个较新的版本, Log4j 2 “,它提供了相对于其前任的重大改进。”

You can use JNA to write to the Event Log directly without the need of any native DLLs. 您可以使用JNA直接写入事件日志,而无需任何本机DLL。 See Advapi32 and Advapi32Util classes for various event log methods (ships since JNA 3.2.8). 有关各种事件日志方法,请参阅Advapi32和Advapi32Util类(自JNA 3.2.8以来发布)。

If you're using Log4j, consider Log4jna instead of NTEventLogAppender. 如果您使用的是Log4j,请考虑使用Log4jna而不是NTEventLogAppender。

Back in 2001 JavaWorld published an article on how to write messages to the Windows NT Event Log. 早在2001年,JavaWorld就发布了一篇关于如何将消息写入Windows NT事件日志的文章。 Or, you can take a look at the Log4j NTEventLogAppender class. 或者,您可以查看Log4j NTEventLogAppender类。

You can also use the eventcreate command on Windows XP Pro and above. 您还可以在Windows XP Pro及更高版本上使用eventcreate命令。

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

Runtime.getRuntime().exec(command);

For XP home and lower, you could create a vbs application that writes using the wscript.shell.eventcreate method. 对于XP home和更低版本,您可以创建一个使用wscript.shell.eventcreate方法写入的vbs应用程序。 However you sacrifice the ability to specify source. 但是你牺牲了指定源的能力。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM