NTEventLogAppender的源代码示例?

时间:2012-06-25 12:24:27

标签: logging log4j event-log event-viewer

我正在尝试学习如何从java eclipse登录到事件查看器,我已经阅读了关于堆栈溢出的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)

应该是什么来源?

如果有人能够展示这样一个非常好的程序的完整例子。请尽可能详细地说明,谢谢。

我现在的代码是:

package Output;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.nt.NTEventLogAppender;

public class EventLog {
    public static void main(String[] args) 
    {   

        Logger myLogger = Logger.getLogger(EventLog.class);

        String mySource = "the source";
        PatternLayout myLayout = new PatternLayout("[%c][%l][%p][%thread]: %m%n");

        NTEventLogAppender eventLogAppender= new NTEventLogAppender(mySource,myLayout);

        ConsoleAppender consoleAppender= new ConsoleAppender(myLayout);

        myLogger.addAppender(consoleAppender);
        myLogger.addAppender(eventLogAppender);

        myLogger.setLevel(Level.WARN);

        myLogger.fatal("Come on print");
    }    
}

2 个答案:

答案 0 :(得分:2)

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

“不要忘记在Windows系统的PATH上的目录中放置NTEventLogAppender.dll,NTEventLogAppender.amd64.dll,NTEventLogAppender.ia64.dll或NTEventLogAppender.x86.dll。否则,您将获得一个java.lang.UnsatisfiedLinkError“

答案 1 :(得分:0)

使用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);
        }
    }

}

`