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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/164879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:58:37  来源:igfitidea点击:

How to write from Java to the Windows Event Log?

javawindowsloggingevent-log

提问by Svet

How can I write from Java to the Windows Event Log?

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

采纳答案by Lou Franco

Log4J is a Java-based logging utility. The class NTEventLogAppendercan be used to "append to the NT event log system". See the documentation here:

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

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“比其前身提供了显着的改进。”

回答by TomC

Back in 2001 JavaWorld published an article on how to write messages to the Windows NT Event Log.Or, you can take a look at the Log4j NTEventLogAppenderclass.

早在 2001 年,JavaWorld 就发表了一篇关于如何将消息写入 Windows NT 事件日志的文章。或者,您可以查看 Log4j NTEventLogAppender类。

回答by Andrew

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. However you sacrifice the ability to specify source.

对于 XP 家庭版和更低版本,您可以创建一个使用 wscript.shell.eventcreate 方法编写的 vbs 应用程序。但是,您牺牲了指定来源的能力。

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

回答by dB.

You can use JNAto write to the Event Log directly without the need of any native DLLs. See Advapi32 and Advapi32Util classes for various event log methods (ships since JNA 3.2.8).

您可以使用JNA直接写入事件日志,而无需任何本机 DLL。请参阅 Advapi32 和 Advapi32Util 类以了解各种事件日志方法(自 JNA 3.2.8 起提供)。

If you're using Log4j, consider Log4jnainstead of NTEventLogAppender.

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