One of our products is currently using IKVM to interop with a java library. I kept seeing console warnings saying:
log4j:WARN Please initialize the log4j system properly.
Clearly the java library uses log4j and it is not getting initialized by my application. A customer was rightfully confused by this message so I spent a little time getting log4j setup and working.
At first I was hoping I could host log4j configuration XML along side my log4net configuration but sadly that was a pipedream. I opted for this approach.
public const string Log4JConfigFileName = "log4j.config";
private static void ConfigureLog4J()
{
if(File.Exists(Log4JConfigFileName))
{
PropertyConfigurator.configureAndWatch(Log4JConfigFileName);
return;
}
var patternLayout = new PatternLayout("%d [%t] %-5p %c - %m%n");
var consoleAppender = new ConsoleAppender(patternLayout);
var fileAppender = new RollingFileAppender(patternLayout, "./logs/file-extraction-errors.log", true);
fileAppender.setMaxFileSize("1MB");
fileAppender.setMaxBackupIndex(5);
var rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.ERROR);
rootLogger.addAppender(fileAppender);
rootLogger.addAppender(consoleAppender);
}
Look for an existing log4j.config log file use that when it is present. Otherwise configure a console and a file appender and log only Errors to a particular file. The manual a lot.
Our log4j usage is pretty vertical so I can get away with hard coding the defaults and rely on throwing a log4j configuration file into the application directory when we need better control.
Hope this post helps if you ever have the pleasure of interop with log4j.




Post new comment