I have a C# app and when I try to direct log output to 1 log file, it writes to both log files.
c# code:
public static ILog log { get; set; }
log = LogManager.GetLogger("log1");
log.Info("foo");
app.config:
<log4net>
<logger name="log1" additivity="false">
<appender-ref ref="RollingLogFileAppender1" />
</logger>
<logger name="log2" additivity="false">
<appender-ref ref="RollingLogFileAppender2" />
</logger>
<appender name="RollingLogFileAppender1" type="log4net.Appender.RollingFileAppender">
<file value="log1.txt" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<appender name="RollingLogFileAppender2" type="log4net.Appender.RollingFileAppender">
<file value="log2.log" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
</log4net>
This happens whether I try log1 or log2. It writes to both regardless.
Anyone know how to resolve this? Thanks!
Related
How wil get Context Path in log4j property files? Ex: My log file name would be contextpath.log.
You will have to create a different appender for each context path. And Implement each context in a different Controller class.
for example
<appender name="FILE-1" class="org.apache.log4j.FileAppender">
<param name="File" value="contextpath1.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d::[%t]::%-5p::%c::%x - %m%n" />
</layout>
</appender>
<appender name="FILE-2" class="org.apache.log4j.FileAppender">
<param name="File" value="contextpath2.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d::[%t]::%-5p::%c::%x - %m%n" />
</layout>
</appender>
<logger name="com.mycompany.apackage.Context1Class">
<level value="info"/>
<appender-ref ref="FILE-1"/>
</logger>
<logger name="com.mycompany.apackage.Context2Class">
<level value="info"/>
<appender-ref ref="FILE-2"/>
</logger>
Log4j logging doesn't work on wildfly-10.1.0.Final, even logfile is not created. Application seems to be working
my log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/local/icm/iban-konverter/logs/iban-konverter-log.txt"/>
<param name="Encoding" value="ISO-8859-1"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="3"/><!-- define how many backup files to keep -->
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<logger name="com.bsbanksysteme">
<level value="debug"/>
</logger>
<root>
<priority value ="error"/>
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
I've got the following configuration using log4net. The problem is that the logger in my C# code is now logging the errors into the two log files. I've got two different service classes in the same windows service. I initialize the logger in one service using this line:
private static readonly ILog _logger = LogManager.GetLogger(typeof(EmployeeImportService));
But when this service runs and logs, it's writing to both log files.
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\Temp\HRFiles\Sharp\Log\Log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="OvertimeLogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\Temp\HRFiles\YTD\Log\Log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="OvertimeLogFileAppender" />
</root>
</log4net>
How do I configure it so that each service writes to it's own log file? I read that the logger tag in the configuration has an additivity attribute that solves this problem but I have no logger element in my configuration.
You can have two separate loggers, and define which appenders each logger will use in the configuration: you do have to declare additivity=false or else the loggers inherit the appenders from the root logger.
So, with made-up names:
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="OvertimeLogFileAppender" />
</root>
<logger name="Company.Project.EmployeeImportService" additivity="false">
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="Company.Project.EmployeeOvertimeService" additivity="false">
<appender-ref ref="OvertimeLogFileAppender" />
</logger>
Then in your service classes you get the approppriate logger:
// General log - EmployeeImportService
ILog logger = LogManager.GetLogger(typeof(EmployeeImportService));
// Log overtime - EmployeeOvertimeService
ILog logger = LogManager.GetLogger(typeof(EmployeeOvertimeService));
Oups yes I did a mistake. But it quiet the same. You have to create filter in each appender node
<filter type="log4net.Filter.LoggerMatchFilter">
<!-- allows this sub-namespace to be logged... -->
<loggerToMatch value="EmployeeImportService" />
</filter>
I am new to log4net. Here is my app.config. What is happening is at a point after going through a switch statement, my logger "X" stops writing to my log file. I tried x.Logger.Repository.Shutdown();, and all that does is stop my "Y" from even logging. My suspicion is that the file is in use, and can't be written to (the next logging statement does begin in a different class. Again, I'm new. Basically, it writes about 10 lines, goes to another class with a logger of the same name, works in that class for a few more lines, and then when it goes back to the original class, it stops logging. I'm a little lost. P.S. I have the correct section name, and I am calling my log as such:
private static readonly ILog eventLog = LogManager.GetLogger("EventLog");
<appender type="log4net.Appender.FileAppender" name="event">
<file value="C:X.log" />
<layout type="log4net.Layout.PatternLayout">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender type="log4net.Appender.FileAppender" name="cLog">
<file value="Y.log" />
<layout type="log4net.Layout.PatternLayout">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name="EventLog">
<level value="INFO" />
<maximumfilesize value="256KB" />
<param value="INFO" name="Threshold" />
<appender-ref ref="event" />
</logger>
<logger name="crLog">
<level value="INFO" />
<maximumfilesize value="256KB" />
<param value="INFO" name="Threshold" />
<appender-ref ref="cLog" />
</logger>
As it turns out, when you call x.Logger.Repository.Shutdown(); it kills any logs that you might want to write to at that point in time.
I will either move x.Logger.Repository.Shutdown(), or find a different method for releasing my files so the last portion of my app can use (email) the file.
I get this error "log4j:ERROR Failed to rename" in my tomcat server log.
I have multiple DailyRollingFileAppender in my log4j XML. And each appender points to different file. Only once instance of the application is running.
I used Handle utility and run the command (with log file name as parameter)
handle -f ams-app.log
and the result is
Tomcat7.exe pid: 5032 type: File 54C: E:\apps\ams\logs\ams-app.log
From this I assume there is only one lock on the file.
My environment Windows Server 2008, tomcat 7.0 and Java 6 and slf4j-log4j12(1.5.6)
This problem was not happening when same application hosted on AIX server.
I googled this problem and it seems to be windows specfic.
Also I am not keen to use DatedFileAppenders or any forked log4j.
Does logback solve such issues.
Any suggestions on how to solve this problem is appreciated.
Thanks
here my log4j xml
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE_APPENDER" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n %d{yyyy/MM/dd HH:mm:ss} %-5p %x %M(%F:%L) - %m %n"/>
</layout>
</appender>
<appender name="APP_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="datePattern" value="yyyy-MM-dd"/>
<param name="file" value="E:/apps/ams/logs/ams-app.log"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n %d{yyyy/MM/dd HH:mm:ss} %-5p %x %M(%F:%L) - %m %n"/>
</layout>
</appender>
<appender name="SQL_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="datePattern" value="yyyy-MM-dd"/>
<param name="file" value="E:/apps/ams/logs/ams-sql.log"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n %d{yyyy/MM/dd HH:mm:ss} %-5p %x %M(%F:%L) - %m %n"/>
</layout>
</appender>
<appender name="Quartz_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="datePattern" value="yyyy-MM-dd"/>
<param name="file" value="E:/apps/ams/logs/ams-Quartz.log"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n %d{yyyy/MM/dd HH:mm:ss} %-5p %x %M(%F:%L) - %m %n"/>
</layout>
</appender>
<logger name="org.springframework"><level value="WARN"/></logger>
<logger name="flex.messaging.log.ServletLogTarget"><level value="WARN" /></logger>
<logger name="net.sf.jasperreports"><level value="WARN" /></logger>
<logger name="net.sf.jasperreports.engine.fill"><level value="WARN" /></logger>
<logger name="org.apache.commons.beanutils"><level value="WARN" /></logger>
<logger name="jdbc.sqltiming" additivity="false"><level value="DEBUG"/><appender-ref ref="SQL_APPENDER"/></logger>
<logger name="org.quartz" additivity="false"><level value="WARN" /><appender-ref ref="Quartz_APPENDER"/></logger>
<logger name="org.quartz.SchedulerException"><level value="ERROR" /><appender-ref ref="MAIL_APPENDER"/></logger>
<logger name="org.hmil.ams.BatchDAO" additivity="false"><level value="INFO"/><appender-ref ref="Quartz_APPENDER"/></logger>
<root>
<priority value="debug"></priority>
<appender-ref ref="APP_APPENDER" />
<appender-ref ref="CONSOLE_APPENDER"/>
</root>
It appears that if you're running windows this is a known bug:
see here:
http://do.whileloop.org/2014/02/14/log4j-rolling-file-appenders-in-windows/
See here:
https://issues.apache.org/bugzilla/show_bug.cgi?id=29726
And also here:
http://www.coderanch.com/t/424837/java/java/Log-log-file-rolled-day
I had the same issue and fixed it by updating log4j to version 1.2.15 as per this discussion