3

I'm trying to learn how to log from java eclipse to event viewer and I've read the api, various sites and similar questions on stack overflow.

But when I follow the steps I always get:

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)

What is the source supposed to be?

if anyone could show a full example of such a program that would be excellent. Please go into as much detail as possible, thanks.

My code right now is:

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");
    }    
}
Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
user1479897
  • 145
  • 2
  • 11

2 Answers2

2

I was just looking into how to do this also. I have not had enough time to test this answer, but from what I have read the error is caused by not having the proper .dll file in the right spot.

"Do not forget to place NTEventLogAppender.dll, NTEventLogAppender.amd64.dll, NTEventLogAppender.ia64.dll or NTEventLogAppender.x86.dll as appropriate in a directory that is on the PATH of the Windows system. Otherwise, you will get a java.lang.UnsatisfiedLinkError"

j0k
  • 22,600
  • 28
  • 79
  • 90
Scott
  • 21
  • 2
1

Instead of using this ,use eventcreate.exe command ...it does the same work and is much more easier`

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

}

`

oreokuchi
  • 11
  • 3