Using multiple log4net file loggers - log4net

I have file appenders FileA, FileB, and FileC. FileA I add to the root element as I want it to be a catch all, (more on this below). FileB and FileC I use for specific messages and create named loggers for each of those appenders. In code, I load the log I'm using for most messages like so:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
The other loggers, I load like this
private static readonly log4net.ILog commandLog = log4net.LogManager.GetLogger("LoggerFileB");
What's happening is I'm getting what I expect in LoggerFileB, ie, ONLY the special messages. The problem is these messages also showing up in LoggerFileA, my catch-all I added to root. I could create a specific named instance for the catch-all, instead of adding it to the root element, but I want the calling type as the logger name in the output. Creating a named logger means that %logger outputs the name of the log instead of the type. Is there a way to get precisely what I want (the catchall to show the logger name as the type, but not show messages logged to other named loggers)? Hopefully I'm missing something and there is a simple solution.
Here's an example of what my log.config looks like for this situation.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="FileA" type="log4net.Appender.RollingFileAppender">
<file value="FileA.txt" />
...snip...
</appender>
<appender name="FileB" type="log4net.Appender.RollingFileAppender">
<file value="FileB.txt" />
...snip...
</appender>
<appender name="FileC" type="log4net.Appender.RollingFileAppender">
<file value="FileC.txt" />
...snip...
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LoggerFileA" />
</root>
<logger name="LoggerFileB">
<level value="ALL" />
<appender-ref ref="FileB" />
</logger>
<logger name="LoggerFileC">
<level value="ALL" />
<appender-ref ref="FileC" />
</logger>
</log4net>
</configuration>

You can use set the additivity to false:
<logger name="LoggerFileB" additivity="false">

Related

Log4net multiple loggers - File logger not working

Following configuration logs to Eventlog and it works for SmtpSender. But the filelogger does not log, it creates the log file in the path, but does not do any debug logs.
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
But when I change the root logger to RollingFileAppender it logs to file,
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
Any idea why this happens? How can I get file logger working in this scenario.
You did not post the appender confuriguration, however the easiest way to figure out what goes wrong is enable internal debugging. This will tell you what goes wrong in the rolling file appender:
There are 2 different ways to enable internal debugging in log4net.
These are listed below. The preferred method is to specify the
log4net.Internal.Debug option in the application's config file.
• Internal debugging can also be enabled by setting a value in the
application's configuration file (not the log4net configuration file,
unless the log4net config data is embedded in the application's config
file). The log4net.Internal.Debug application setting must be set to
the value true. For example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
This setting is read immediately on startup an will cause all internal
debugging messages to be emitted.
• To enable log4net's internal debug programmatically you need to set
the log4net.Util.LogLog.InternalDebugging property to true. Obviously
the sooner this is set the more debug will be produced.
Internal debugging messages are written to the console and to the
System.Diagnostics.Trace system. If the application does not have a
console the messages logged there will be lost. Note that an
application can redirect the console stream by setting the
System.Console.Out. The Trace system will by default send the message
to an attached debugger (where the messages will appear in the output
window). If the process does not have a debugger attached then the
messages are sent to the system debugger. A utility like DebugView
from http://www.sysinternals.com may be used to capture these
messages.
As log4net internal debug messages are written to the
System.Diagnostics.Trace system it is possible to redirect those
messages to a local file. You can define a trace listener by adding
the following to your application's .config file:
<configuration>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
...
</configuration>
Make sure that the process running your application has permission to write to this file.
log4net faq
I managed to get the logging work, in fact I did this as an experiment and it worked.
Added RollingFileAppender to the root element,
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="RollingFileAppender"/>
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>

log4j not showing info on the console

I'm new to log4j and trying to use it in a project. For some reason the info won't get displayed on the console. It only works when i change it to logger.error(). This is only happening in the userServiceImpl class the other classes like Controllers are fine.
This is the log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false"
xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- This default ConsoleAppender is used to log all NON perf4j messages
to System.out -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
</layout>
</appender>
<!-- The root logger sends all log statements EXCEPT those sent to the perf4j
logger to System.out. -->
<root>
<level value="ERROR" />
<appender-ref ref="console" />
</root>
<logger name="com.click.heal.controller" additivity="false" >
<level value="INFO" />
<appender-ref ref="console"/>
</logger>
<logger name="com.click.heal.service" additivity="true" >
<level value="INFO" />
<appender-ref ref="console"/>
</logger>
</log4j:configuration>
Your root category is at ERROR level. Anything that doesn't fall under one of your other two loggers will fall under root, and will only log at ERROR level. If you change your root level to INFO, I bet it logs, right? Not saying that's what you want, but it would be a clue that your UserServiceImpl class is logging under root right now.
EDIT
Try turning additivity to false on your logger for the service package. additivity means the messages propogate to the parent, and since the parent is root and root logs to the same console appender, it seems likely that this is causing the problem.

Log4j configuration

I have two appenders, file and console, in my project. I would like to configure my application to perform as such:
all loggers with name "my.app.*":
1. log events DEBUG and higher to fileA
2. log events DEBUG and higher to fileB
all other loggers:
1. log events WARN and higher to fileA
2. log events DEBUG and higher to fileB
Ideally, the configuration would look something like this:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="fileA" class="org.apache.log4j.FileAppender">
<!-- configuration -->
</appender>
<appender name="fileB" class="org.apache.log4j.FileAppender">
<!-- configuration -->
</appender>
<logger name="my.app" additivity="false">
<level="DEBUG"/>
<appender-ref ref="fileA"/>
</logger>
<logger name="" additivity="true">
<level="DEBUG"/>
<appender-ref ref="fileB"/>
</logger>
<root>
<level="WARN"/>
<appender-ref ref="fileA"/>
</root>
</log4j>
However, this setup causes loggers named "my.app" to only log to console, and all other loggers to log to console on WARN and above. Essentially, <logger name=""> is being ignored. Is there another way to emulate this behavior with log4j?
PS. I apologize for the poor formatting, really struggling to get this to work tonight :/
You need:
<logger name="my.app" additivity="false">
<level="DEBUG"/>
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</logger>
<root>
<appender-ref ref="console-warn"/>
<appender-ref ref="file-debug"/>
</root>
For the root-appender you need two new console/file appenders, that have the desired level restrictions.
<appender name="file-debug" class="org.apache.log4j.FileAppender">
<param name="Threshold" value="DEBUG"/>
</appender>
<appender name="console-warn" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="WARN"/>
</appender>

log4j:ERROR Attempted to append to closed appender named [..]

I am getting following errors on my console repeatedly
log4j:ERROR Attempted to append to closed appender named [ConsoleAppender].
log4j:ERROR Attempted to append to closed appender named [FixedWindowRollingFile].
used log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender class="org.apache.log4j.rolling.RollingFileAppender" name="FixedWindowRollingFile">
<param name="Append" value="true"/>
<param name="ImmediateFlush" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
<param name="fileNamePattern" value="logs/StandardizeAccountService.%i.log"/>
<param name="minIndex" value="1"/>
<param name="maxIndex" value="10"/>
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="1002400"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout"/>
</appender>
<logger name="com.arosys" additivity="false" >
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FixedWindowRollingFile"/>
</logger>
<root>
<priority value="INFO"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="FixedWindowRollingFile"/>
</root>
</log4j:configuration>
please help me where the problem.
I got the same error:
log4j:ERROR Attempted to append to closed appender named [rollingFileAppender].
In my log4j.xml
I have two loggers with the same name like below
<logger name="java.sql.PreparedStatement" additivity="false">
<level value="INFO"/>
<appender-ref ref="rollingFileAppender"/>
</logger>
<logger name="java.sql.PreparedStatement">
<level value="INFO"/>
<appender-ref ref="rollingFileAppender"/>
</logger>
I removed the duplicate, it worked.
I've answered similar question here: https://stackoverflow.com/a/9973283/340290
In my case, I've two log4j.properties available to the Log4J: one via placing it in classpath and other being loaded programmatically (using PropertyConfigurator.configure(..)).
And in the two files, I've ConsoleAppender registered with same name stdout and used for same category twice (one per each properties file). Removing config or the properties file solved my issue.
One could overwrite the configuration using:
BasicConfigurator.resetConfiguration();
PropertyConfigurator.configure(props);
Just to clarify because I was mislead by MaDa answer, additivity=false redirects the output to another place than the default (root logger) and NOT to the default.
See http://logging.apache.org/log4j/1.2/manual.html chapter "Appenders and Layouts"
This may also mean that you already have your server running, and you're trying to run it again. The second instance can't write to the log file because it's already open in your server.
solution: check to see if your server is already running, and restart it if neccessary.
In my case, I added by mistake 2 "root" elements.
I'm not saying this is the cause of the behaviour you describe (but it might), but this part:
<logger name="com.arosys" additivity="false" >
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FixedWindowRollingFile"/>
</logger>
is pointless. Normally, you'd set a non-additive logger to redirect it somewhere else than the default place (your root logger), but you still send the output to the default place. You might as well delete this fragment.

Log4Net: Multiple loggers

I have the following log4net configuration:
<log4net>
<appender name="A1" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\path1.log" />
</appender>
<appender name="A2" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\path2.log" />
</appender>
<logger name="A1Logger">
<level value="ALL" />
<appender-ref ref="A1" />
</logger>
<logger name="A2Logger">
<level value="ALL" />
<appender-ref ref="A2" />
</logger>
</log4net>
and then in code I do the following:
var logger1 = LogManager.GetLogger("A1Logger");
var logger2 = LogManager.GetLogger("A2Logger");
but both log to the same file C:\path1.log.
What am I doing wrong?
Add the debug="true" attribute to the <log4net> element, my guess is that it is the missing <root> element that causes problems. You should always include the <root /> logger element.
Seems that it was a weird debugging error. Right now it doesn't reproduce. My apologies.

Resources