Answered by:
Log4Net RollingFileAppender multiple files

Question
-
User-1565286790 posted
I am attempting to use Log4Net RollingFileAppender and set the configuration via C#. I wish to write to several different log files, different data to each log file. Each log file has a class with code like the one below. What is happening is that the code is writing the same logging data across all of the log files (5 files) when it should be writing certain parts to each file. Can someone point me in the write direction as to what I am missing in the code below?
private static bool isConfigured = false; private static ILog iLog; private static void ConfigureCS() { try { if (isConfigured) return; log4net.GlobalContext.Properties["LogName"] = Toggles.strGetLogCalendarSettingsFileName; var loggerName = "RFA_CS"; // computerName; var loggerCS = (log4net.Repository.Hierarchy.Logger)log4net.LogManager.GetRepository().GetLogger(loggerName); loggerCS.Additivity = true; //Add the default log appender if none exist if (loggerCS.Appenders.Count == 0) { //directory to deposit log string strGetLogDIR = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\"; var directoryName = strGetLogDIR + Toggles.strGetPinPointTitle + "\\"; //If the directory doesn't exist then create it if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } var fileName = Path.Combine(directoryName, Toggles.strGetLogCalendarSettingsFileName); //Create the rolling file appender var appender = new log4net.Appender.RollingFileAppender(); appender.Name = "RFA_CS"; appender.File = fileName; appender.StaticLogFileName = true; appender.AppendToFile = true; appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Size; appender.MaxSizeRollBackups = Toggles.intGetMaxSizeRollBackups; // 4; appender.MaximumFileSize = Toggles.strGetLoggingMaxSize; // "1MB"; appender.PreserveLogFileNameExtension = true; //Configure the layout of the trace message write var layout = new log4net.Layout.PatternLayout() { ConversionPattern = "%date{yyyy-MM-dd hh:mm:ss} %-5level - %message%newline" //ConversionPattern = "%date{hh:mm:ss.fff} [%thread] %-5level - %message%newline" //ConversionPattern = "%date{hh:mm:ss ttt} - %message%newline" }; appender.Layout = layout; layout.ActivateOptions(); //Let log4net configure itself based on the values provided appender.ActivateOptions(); log4net.Config.BasicConfigurator.Configure(appender); } iLog = LogManager.GetLogger("RFA_CS"); isConfigured = true; //Info("Logging Configured at " + DateTime.Now.ToString("g")); } catch (Exception ex) { } } //public static event EventHandler<ExceptionLoggedEventArgs> ExceptionLogged; public static void Debug(object message) { ConfigureCS(); iLog.Debug(message); } public static void Debug(object message, Exception exception) { ConfigureCS(); iLog.Debug(message, exception); } public static void Error(object message) { ConfigureCS(); iLog.Error(message); } public static void Error(object message, Exception exception) { ConfigureCS(); iLog.Error(message, exception); } public static void Fatal(object message) { ConfigureCS(); iLog.Fatal(message); } public static void Fatal(object message, Exception exception) { ConfigureCS(); iLog.Fatal(message, exception); } public static void Info(object message) { ConfigureCS(); iLog.Info(message); } public static void Info(object message, Exception exception) { ConfigureCS(); iLog.Info(message, exception); } public static void Warn(object message) { ConfigureCS(); iLog.Warn(message); } public static void Warn(object message, Exception exception) { ConfigureCS(); iLog.Warn(message, exception); }
Friday, April 18, 2014 2:18 PM
Answers
-
User-1454326058 posted
Hi niyack,
Please refer to this thread to this thread to append the RollingFileAppender.
# Can you configure log4net in code instead of using a config file?
There is a link may benefit you:
# Use Multiple log4net Outputs from One Application
http://www.codeproject.com/Articles/18720/Use-Multiple-log-net-Outputs-from-One-Application
Thanks
Best Regards
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 21, 2014 12:20 AM