The following code reads and prints the events from the Windows Event Log of the local computer.
import java.io.UnsupportedEncodingException;
import java.util.Date;
import com.sun.jna.platform.win32.Advapi32Util.EventLogIterator;
import com.sun.jna.platform.win32.Advapi32Util.EventLogRecord;
import com.sun.jna.platform.win32.WinNT;
public class EventLog {
public static void main(String[] args) throws UnsupportedEncodingException {
EventLogIterator iter = new EventLogIterator(null, "Application", WinNT.EVENTLOG_FORWARDS_READ);
while (iter.hasNext()) {
EventLogRecord record = iter.next();
if (record.getData() == null) {
continue;
}
Date eventTime = new Date(record.getRecord().TimeGenerated.longValue() * 1000L);
String data = new String(record.getData(), "windows-1252");
System.out.println(record.getRecordNumber() + " Event Time: " + eventTime + //
": Event ID: " + record.getInstanceId() + //
", Event Type: " + record.getType() + //
", Event Data: " + data + //
", Event Source: " + record.getSource());
}
}
}
The problem is that the event data is present as byte array (byte[]) but I need proper formatted text. I tried to transform this data into a String by using different encodings but none of them produced the expected result.
String data = new String(record.getData(), "windows-1252");
How can the event data be transformed into human readable text?