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.
Related
I'm trying to define 2 independent appenders to log into to 2 files. I define
DEBUG level for the "DebugAppender" and then for the "RelevantAppender" I define different levels for the "Security" and "ServerStats" loggers.
The thing is that these loggers definitions are overwriting my "DebugAppender" levels and now it's not in DEBUG mode (all the loggers).
How can make it work as expected?
<log4net>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<file value="plastic.debug.log.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value=".yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %thread %property{TransactionID} %property{ClientMachine} %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="RelevantAppender" type="log4net.Appender.RollingFileAppender">
<file value="plastic.relevant.log.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value=".yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %property{ClientMachine} %-5level %logger - %message%newline" />
</layout>`enter code here`
</appender>
<logger name="Security">
<level value="ERROR" />
<appender-ref ref="RelevantAppender" />
</logger>
<logger name="ServerStats">
<level value="INFO" />
<appender-ref ref="RelevantAppender" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="DebugAppender" />
</root>
You're problem is, that the definition you make in Logger outweights all following levels. So here comes my solution:
<log4net>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<file value="plastic.debug.log.txt" />
<!--...-->
</appender>
<appender name="Security_RelevantAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="ERROR" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="plastic.relevant.log.txt" />
<!--...-->
</appender>
<appender name="Serverstat_RelevantAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--...-->
</appender>
<logger name="Security">
<appender-ref ref="Security_RelevantAppender" />
</logger>
<logger name="ServerStats">
<appender-ref ref="Serverstat_RelevantAppender" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="DebugAppender" />
</root>
</log4net>
Define 3 different appenders. The DebugAppender's level is defined by <root>. The two relevant appenders write both to the same file with different log-levels (thresholds). It is important, that you include the locking model <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />. Utherwise the first appender you call will lock the file and the second one can not write in it (Can Log4net have multiple appenders write to the same file?). In the <logger> you just define the two different appenders and no loglevel. This way it will take the level from the appender settings.
When you do it like this all events of the type debug+ from any logger will be written to plastic.debug.log.txt. To plastic.relevant.log.txt only events error+ from security-logger and info+ from serverstat-logger are written.
Hope this helps
I have a config file with two appenders, one file appender and one database appender. I want to log everything to the file appender, and only log exceptions to the database appender. When setting up both appenders in the section it logs fine but all log events are sent to both appenders, which is not what i want.
I changed the configuration but with this current configuration, exceptions get logged to the database, and nothing is getting written to the file appender. Can anyone tell me why I am not getting anything written to the file appender?
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Log4net\Workflow\TestLog.txt" />
<threshold value="All" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p {%logger} %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<!-- Removed to keep this snippet simple-->
</appender>
<root>
<level value="Error" />
<appender-ref ref="AdoNetAppender" />
</root>
<logger name="AllLogs">
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
What you have here is the following:
all logs events with the level Error or more will go to the AdoNetAppender
all logs events originating from a logger whose name is based on AllLogs will go to the RollingLogFileAppender
From what I understand you want all logs to default to the file, and only error ones to go also to the database. Then simply add both appenders to your root logger so that both get all events, and add filters to only let filters you're interested in pass through: a level range filter on your database appender would work
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!-- rest of config snipped to save space -->
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<!-- rest of config snipped to save space -->
</appender>
<root>
<appender-ref ref="AdoNetAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
Of course if you don't want any duplicate just filter errors and above in the rolling file appender
I must log some methods. In one method I must log everything. In all others method I've just log exception...
I have done this configuration:
<log4net>
<appender name="OneMethodFileAppender" type="log4net.Appender.RollingFileAppender">
...
<filter type="log4net.Filter.PropertyFilter">
<key value="realMethod" />
<stringToMatch value="xxx" />
<acceptOnMatch value="true" />
</filter>
</appender>
<appender name="WebsiteFileAppender" type="log4net.Appender.RollingFileAppender">
...
<filter type="log4net.Filter.PropertyFilter">
<key value="realMethod" />
<stringToMatch value="xxx" />
<acceptOnMatch value="false" />
</filter>
</appender>
<root>
<appender-ref ref="WebsiteFileAppender">
<threshold value="ERROR" />
</appender-ref>
<appender-ref ref="OneMethodFileAppender">
<threshold value="INFO" />
</appender-ref>
</root>
</log4net>
So, when I match the property realMethod I should use the appender OneMethodFileAppender that log at the INFO level; in all other cases i should use the appender WebsiteFileAppender that log at ERROR level.
I have a different output.
1. When i match the method xxx everything works correctly. I mean that a log at INFO level in the correct log.
2. But when I do not match the method xxx, I log in both files and both at INFO level.
I would suggest to use the logger element to configure your logging:
<log4net>
<appender name="OneMethodFileAppender" type="log4net.Appender.RollingFileAppender">
...
</appender>
<appender name="WebsiteFileAppender" type="log4net.Appender.RollingFileAppender">
...
</appender>
<root>
<appender-ref ref="WebsiteFileAppender">
<threshold value="ERROR" />
</appender-ref>
</root>
<logger name="LoggerName1">
<level value="INFO" />
<appender-ref ref="WebsiteFileAppender" />
</logger>
<logger name="LoggerName2">
<level value="INFO" />
<appender-ref ref="OneMethodFileAppender" />
</logger>
</log4net>
Both methods should have there own logger:
logger = LogManager.GetLogger("logger1");
I'm using Apache commons HTTPClient with Apache Axis 1.5 and I'm trying to log the messages exchanged when making Web Service calls by enabling org.apache.commons.httpclient to DEBUG and httpclient.wire to DEBUG. However, this doesn't work. Mentioned below is my log4j.xml - can someone help me?
Thanks
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="rolling" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="test.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c:%L - %m%n"/>
</layout>
</appender>
<logger name="org.apache.commons.httpclient">
<level value="DEBUG"/>
</logger>
<logger name="httpclient.wire">
<level value="DEBUG"/>
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="rolling"/>
</root>
</log4j:configuration>
You need to have the log level set at ALL, not DEBUG
<logger name="httpclient.wire">
<level value="ALL"/>
</logger>
Try putting a priority value set to DEBUG to your <root> node in log4j.xml
<root>
<level value="DEBUG" />
<priority value="DEBUG" />
<appender-ref ref="rolling"/>
</root>
When I compiled my application in release mode, I found that the Log4Net still logs debug information; any idea how to fix this?
This is my App.Config file:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\Documents and Settings\test\Application Data\Log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} - %m%n" />
</layout>
</appender>
</log4net>
Did I miss anything?
There's nothing in your App.Config file to tell log4net to do things differently in release or debug mode. If you want logging to be different between the two builds, you have to change your configuration file between the two builds.
Your best bet is probably to create one App.Config for Release, one for Debug, and then follow the advice in the StackOverflow question:
Deploy an app.config based on build configuration
NOTE: The difference between your release and debug App.Config will be the following line in the debug version
<level value="DEBUG" />
versus the following line in the release version (or of course you could choose ERROR or FATAL if you want):
<level value="INFO" />
Maybe try something like this instead? Set to whatever minimum level you want to receive.
<level value="WARN" />
If your App.Config looks like this:
<root>
<level value="Info" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
You can modify the log level by code (put the code in Program.cs):
#if DEBUG
log4net.Repository.ILoggerRepository RootRep;
RootRep = LogManager.GetRepository(Assembly.GetCallingAssembly());
XmlElement section = ConfigurationManager.GetSection("log4net") as XmlElement;
XPathNavigator navigator = section.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("root/level");
foreach (XPathNavigator appender in nodes)
{
appender.MoveToAttribute("value", string.Empty);
appender.SetValue("Debug");
}
IXmlRepositoryConfigurator xmlCon = RootRep as IXmlRepositoryConfigurator;
xmlCon.Configure(section);
#endif