Logging NTEventLogAppender的源示例?

Logging NTEventLogAppender的源示例?,logging,log4j,event-log,event-viewer,Logging,Log4j,Event Log,Event Viewer,我正在尝试学习如何从JavaEclipse登录到事件查看器,我已经阅读了api、各种站点以及关于堆栈溢出的类似问题 但当我遵循这些步骤时,我总是得到: Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.log4j.nt.NTEventLogAppender.registerEventSource(Ljava/lang/String;Ljava/lang/String;)I at org.apache.log

我正在尝试学习如何从JavaEclipse登录到事件查看器,我已经阅读了api、各种站点以及关于堆栈溢出的类似问题

但当我遵循这些步骤时,我总是得到:

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.log4j.nt.NTEventLogAppender.registerEventSource(Ljava/lang/String;Ljava/lang/String;)I
at org.apache.log4j.nt.NTEventLogAppender.registerEventSource(Native Method)
at org.apache.log4j.nt.NTEventLogAppender.<init>(NTEventLogAppender.java:79)
at org.apache.log4j.nt.NTEventLogAppender.<init>(NTEventLogAppender.java:65)

我正在研究如何做到这一点。我没有足够的时间来测试这个答案,但从我所读到的错误是由于没有正确的.dll文件在正确的位置造成的

请不要忘记将NTEventLogAppender.dll、NTEventLogAppender.amd64.dll、NTEventLogAppender.ia64.dll或NTEventLogAppender.x86.dll适当地放在Windows系统路径上的目录中。否则,您将得到java.lang.UnsatisfiedLinkError


使用eventcreate.exe命令,而不是使用此命令…它执行相同的工作,而且更简单`

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class WriteToWindowsEventLog {
   static int id=0;
    public void log(String source,String level,String message)  throws IOException, InterruptedException{
        String osName = System.getProperty("os.name").toUpperCase(Locale.ENGLISH);
        if (!osName.startsWith("WINDOWS")) {
            System.err.println("Not windows");
            return;
        } 
        id++;

        String command = "eventcreate "
                + " /l APPLICATION"
                + " /so \"" + source + "\""
                + " /t " +level
                + " /id " + id
                + " /d \"" + message + "\"";


        Process process = Runtime.getRuntime().exec(command);
        process.waitFor(10, TimeUnit.SECONDS);
        int exitValue = process.exitValue();
        if (exitValue != 0) {
            InputStream errorStream = process.getErrorStream();
            String result = new BufferedReader(new InputStreamReader(errorStream))
                .lines()
                .collect(Collectors.joining("\n"));
            System.err.println(result);
        }
    }

}
`

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class WriteToWindowsEventLog {
   static int id=0;
    public void log(String source,String level,String message)  throws IOException, InterruptedException{
        String osName = System.getProperty("os.name").toUpperCase(Locale.ENGLISH);
        if (!osName.startsWith("WINDOWS")) {
            System.err.println("Not windows");
            return;
        } 
        id++;

        String command = "eventcreate "
                + " /l APPLICATION"
                + " /so \"" + source + "\""
                + " /t " +level
                + " /id " + id
                + " /d \"" + message + "\"";


        Process process = Runtime.getRuntime().exec(command);
        process.waitFor(10, TimeUnit.SECONDS);
        int exitValue = process.exitValue();
        if (exitValue != 0) {
            InputStream errorStream = process.getErrorStream();
            String result = new BufferedReader(new InputStreamReader(errorStream))
                .lines()
                .collect(Collectors.joining("\n"));
            System.err.println(result);
        }
    }

}