Configure Log4net to write to multiple files

These answers were helpful, but I wanted to share my answer with both the app.config part and the c# code part, so there is less guessing for the next person. <log4net> <appender name=”SomeName” type=”log4net.Appender.RollingFileAppender”> <file value=”c:/Console.txt” /> <appendToFile value=”true” /> <rollingStyle value=”Composite” /> <datePattern value=”yyyyMMdd” /> <maxSizeRollBackups value=”10″ /> <maximumFileSize value=”1MB” /> </appender> <appender name=”Summary” … Read more

Log4net rolling daily filename with date in the file name

<appender name=”RollingLogFileAppender” type=”log4net.Appender.RollingFileAppender”> <lockingModel type=”log4net.Appender.FileAppender+MinimalLock”/> <file value=”logs\” /> <datePattern value=”dd.MM.yyyy’.log'” /> <staticLogFileName value=”false” /> <appendToFile value=”true” /> <rollingStyle value=”Composite” /> <maxSizeRollBackups value=”10″ /> <maximumFileSize value=”5MB” /> <layout type=”log4net.Layout.PatternLayout”> <conversionPattern value=”%date [%thread] %-5level %logger [%property{NDC}] – %message%newline” /> </layout> </appender>

Log4Net: Programmatically specify multiple loggers (with multiple file appenders)

This thread at the log4net Dashboard details an approach. To summarize a little, hopefully without ripping off too much code: using log4net; using log4net.Appender; using log4net.Layout; using log4net.Repository.Hierarchy; // Set the level for a named logger public static void SetLevel(string loggerName, string levelName) { ILog log = LogManager.GetLogger(loggerName); Logger l = (Logger)log.Logger; l.Level = l.Hierarchy.LevelMap[levelName]; … Read more

How can I change the file location programmatically?

log4net can handle this for you. Any appender property of type string can be formatted, in this case, using the log4net.Util.PatternString option handler. PatternString even supports the SpecialFolder enum which enables the following elegant config: <appender name=”LogFileAppender” type=”log4net.Appender.RollingFileAppender” > <file type=”log4net.Util.PatternString” value=”%envFolderPath{CommonApplicationData}\\test.txt” /> … </appender> Here’s a unit test that proofs the pudding: [Test] public … Read more

log4net vs. Nlog

I was recently tasked to “prototype up some loggin’” for an upcoming project. I didn’t have any logging framework experience. I researched, ran through tutorials, made toy apps, etc. on Log4Net, NLog, and Enterprise Library for a few days. Came back 3-4 weeks later and put them together into a cohesive demo. Hopefully some of … Read more

Setting a log file name to include current date in Log4j

DailyRollingFileAppender is what you exactly searching for. <appender name=”roll” class=”org.apache.log4j.DailyRollingFileAppender”> <param name=”File” value=”application.log” /> <param name=”DatePattern” value=”.yyyy-MM-dd” /> <layout class=”org.apache.log4j.PatternLayout”> <param name=”ConversionPattern” value=”%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n”/> </layout> </appender>

How to track down log4net problems

First you have to set this value on the application configuration file: <configuration> <appSettings> <add key=”log4net.Internal.Debug” value=”true”/> </appSettings> </configuration> Then, to determine the file in which you want to save the output you can add the following code in the same .config file: <configuration> … <system.diagnostics> <trace autoflush=”true”> <listeners> <add name=”textWriterTraceListener” type=”System.Diagnostics.TextWriterTraceListener” initializeData=”C:\tmp\log4net.txt” /> </listeners> … Read more