log4net - Log using multiple appenders - log4net

I want to log messages to TextBox and also to file. I found an example (http://www.codeproject.com/Articles/43027/Logging-display-and-WPF) that logs to TextBox. I modified the app.config and extend it with another appender.
The config file Looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="false"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\log4net_internal.log"/>
</listeners>
</trace>
</system.diagnostics>
<log4net>
<appender name="NotifyAppender" type="log4netSample.Logging.NotifyAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="c:\PrestAba.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="NotifyAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
When i run the application and write the log Messages, it only logs TextBox. Is there more modification needed?

I believe your file name must be escaped to be read:
<param name="File" value="c:\\PrestAba.log" />
Also, you can condense that to
<file value="c:\\PrestAba.log" />
Which is a bit more readable and more in line with log4net's intentions of making readable and maintainable XML.

Related

Swapping from log4net to Serilog using Common.Logging

A solution already exists that uses log4net with Common.Logging,
we have decided we want to swap out log4net and swap in Serilog.
In App.config
--<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1215">
++<factoryAdapter type="Common.Logging.Serilog.SerilogFactoryAdapter, Common.Logging.Serilog">
Seems easy enough, but then as I continued looking through our existing App.config file I came across less obvious areas of transition.
--<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
++<section name="serilog" type="Serilog.Configuration.LoggerSettingsConfiguration, serilog"/><!--Not sure if equivalent-->
This change does not seem as obviously right and is really just a shot in the dark. In fact, I'm pretty sure it is wrong.
Then there is the actual config of the rolling file appender:
<log4net>
<appender name="LM" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LogFileName}\\Program\Logs\\specificProgram.log" />
<param name="RollingStyle" value="Size" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="5" />
<param name="MaximumFileSize" value="10MB" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{MM-dd-yy HH:mm:ss.fff} [%thread] %-5p - %c{1} - %m%n" />
</layout>
</appender>
<appender name="QuickLog" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LogFileName}\\Program\Logs\\QuickLog.log" />
<param name="RollingStyle" value="Size" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="5" />
<param name="MaximumFileSize" value="10MB" />
<param name="StaticLogFileName" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{MM-dd-yy HH:mm:ss.fff} [%thread] %-5p - %c{1} - %m%n" />
</layout>
</appender>
<logger name="LM">
<level value="TRACE"/>
<appender-ref ref="LM"/>
</logger>
<logger name="QuickLog">
<level value="TRACE"/>
<appender-ref ref="QuickLog"/>
</logger>
</log4net>
I couldn't find any blogs or documentation on swapping from log4net to Serilog that use mainly App.config for settings...
In the code behind, we have a pretty simple initialization
if (null == s_logger || null == quick_logger)
{
log4net.GlobalContext.Properties["LogFileName"] = MachineConfiguration.DataPath;
s_logger = CommonLogging.LogManager.GetLogger("LM");
quick_logger = CommonLogging.LogManager.GetLogger("QuickLog");
}
Serilog also seems to lack a GlobalContext, or at least I haven't found the equivalent yet looking through the library.
Is there any mapping of App.config changes from log4net to Serilog? or somewhere to look up what the equivalent types or params are between the two libraries?
I know I can't be the first person to do this swap, but I am having trouble finding documentation that matches what we currently have in place.

Log4j.Debug=true not priniting anything on weblogic

I am using weblogic 10.3.6 and Iam trying to use log4j1.2 for logging purposes.
I have created log4j.xml in src/main/resources.
But log4j is not creating any file or nor even logging on console.
I tried giving jvm property -Dlog4j.debug=true but even that is not printing anything on console.
Any pointers?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<!-- Console Appender -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</layout>
</appender>
<!-- Info Log File Appender -->
<appender name="info-log" class="org.apache.log4j.FileAppender">
<param name="File" value="C:\\Users\\rajan.d.panchal\\Desktop\\newlog\\info.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="info" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<!-- Error Log File Appender -->
<appender name="error-log" class="org.apache.log4j.FileAppender">
<param name="Append" value="false" />
<param name="File" value="C:\\Users\\abc\\Desktop\\newlog\\error.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="warn" />
<param name="LevelMax" value="fatal" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<logger name="com.jcg.log4j.example" additivity="false">
<appender-ref ref="info-log" />
<appender-ref ref="error-log" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Ok. So I was using JRebel and It was not creating any logs. After disabling JRebel it worked.

log4j: errors to an errorFile

I have a class which events I want to log. Say, there are 2 levels: debug and error. How can I log errors to an errorFile and debug info to a debugFile?
So that I use log4j.xml there is a filter mechanism, but it seems to work only for different categories not inside one class.
Please, help to newbie. =)
You have to define and use two appender with different Threshold like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Error -->
<appender name="ErrorFile"
class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="error"/>
<param name="file" value="log.err" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d %C.%M (%L) - %m%n" />
</layout>
</appender>
<appender name="LogFile"
class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="debug"/>
<param name="file" value="log.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d %C.%M (%L) - %m%n" />
</layout>
</appender>
<logger name="foo.bar" additivity="false">
<level value="debug" />
<appender-ref ref="LogFile" />
<appender-ref ref="ErrorFile" />
</logger>
<root>
<priority value="warn" />
<appender-ref ref="ErrorFile"/>
</root>
</log4j:configuration>
This question is also answered in the log4j FAQs.
Just found the LogToAppenderByLevel solution (here) which may be also helpful for you.

log4net: what am I doing wrong?

Being a log4net newb / boob I just copied lines from an NHibernate example project where I can see the log.txt file is updated. Is there a quick answer why mine isn't creating the file?
Cheers,
Berryl
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I saw another post here saying this should go in AssemblyInfo, but in the example project this is just another line in a static helper class. Not wanting to 'mess with' assemblyInfo I also put this in a static helper, along with the following to actually log in the same static helper class:
private static readonly log4net.ILog _log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
And in app.config, I have
<configSections>
<section
name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"
/>
</configSections>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define an output appender (where the logs can go) -->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender, log4net">
<param name="File" value="log.txt" />
<param name="AppendToFile" value="false" />
<layout type="log4net.Layout.PatternLayout, log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
</layout>
</appender>
<appender name="LogDebugAppender" type="log4net.Appender.DebugAppender, log4net">
<layout type="log4net.Layout.PatternLayout, log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n"/>
</layout>
</appender>
<!-- Setup the root category, set the default priority level and add the appender(s) (where the logs will go) -->
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="LogDebugAppender"/>
</root>
<!-- Specify the level for some specific namespaces -->
<!-- Level can be : ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
<logger name="NHibernate">
<level value="ALL" />
</logger>
</log4net>
You are doing the right thing.
Turn on this setting:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
And paste the resulting output here.

log4net 1.2 RollingFileAppender not working

I'm using log4net v1.2 with a Windows Service App. My RollingFileAppender seems not to work. I'm pasting the logging sections of my service.exe.config below. Can anyone advise where I'm going wrong?
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
.....(lots of other config stuff)
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="D:\\Trinity\\Booking\\OneDay_PostTrade\\logs\\Trinity.log" />
<param name="MaximumFileSize" value="20MB" />
<param name="MaxSizeRollBackups" value="10" />
<param name="StaticLogFileName" value="true" />
<param name="Threshold" value="ALL" />
<param name="RollingStyle" value="Composite" />
<param name="appendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
...(stuff in between)
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
.....(stuff in between)
<logger name="CSFB.PostTradeRulesEngine">
<level value="ALL"/>
</logger>
The user your windows service is running as might not have write permission for the log file.
Another possibility is that you forgot to execute XmlConfigurator.Configure();
try writing:
<log4net debug="true">
it will post all errors to console.
thanks to everyone who responded. I dont know what i changed but my logging has started working fine.
Posting my logging sections. I didnt change anything in the code, except a line in the AssemblyInfo.cs:
[assembly: log4net.Config.Domain(UseDefaultDomain=true)]
Thanks again.:)

Resources