Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write from Java to the Windows Event Log?

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

like image 217
Svet Avatar asked Oct 02 '08 22:10

Svet


People also ask

How do I create a Windows event log?

To generate these logs, please follow the steps listed below: Open "Event Viewer" by clicking the "Start" button. Click "Control Panel" > "System and Security" > "Administrative Tools", and then double-click "Event Viewer" Click to expand "Windows Logs" in the left pane, and then select "Application".

What is event logging in Java?

Event Logging is a Java API for logging audit events conforming to the Event Logging XML Schema. The API uses a generated Java JAXB model of the Event Logging XML Schema.


4 Answers

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

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."

like image 146
Lou Franco Avatar answered Oct 10 '22 05:10

Lou Franco


You can use JNA to 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).

If you're using Log4j, consider Log4jna instead of NTEventLogAppender.

like image 42
dB. Avatar answered Oct 10 '22 05:10

dB.


You can also use the eventcreate command on Windows XP Pro and above.

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.

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

like image 22
Andrew Avatar answered Oct 10 '22 05:10

Andrew


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 NTEventLogAppender class.

like image 26
TomC Avatar answered Oct 10 '22 05:10

TomC