Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:45:48+00:00 2026-06-09T03:45:48+00:00

I would like to create a log4net RollingFileAppender that creates a new file each

  • 0

I would like to create a log4net RollingFileAppender that creates a new file each time the program starts. It can roll if the file gets to big but must roll each time the application is started. The appender should not be added if another appender is configured in the configuration. In essence creating a default log configuration that can be overridden by changing the configuration file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:45:51+00:00Added an answer on June 9, 2026 at 3:45 am

    After piecing together a number of other posts, I finally got this working. The implementation is in a static wrapper class. All you have to do is paste this code into your app, set the location to create the file, and call Log.Configure() when the application starts.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    using System.Windows.Threading;
    using log4net;
    using log4net.Core;
    using log4net.Appender;
    using log4net.Repository.Hierarchy;
    using System.Reflection;
    using System.IO;
    
    namespace [Your Namespace]
    {
        public static class Log
        {
            private static bool isConfigured = false;
            private static ILog iLog;
    
            public static void Configure()
            {
                if (isConfigured)
                    return;
    
                var loggerName = typeof(DiagnosticsManager).FullName;
    
                var logger = (log4net.Repository.Hierarchy.Logger)log4net.LogManager.GetRepository().GetLogger(loggerName);
                var ilogger = log4net.LogManager.GetRepository().GetLogger(loggerName);
    
                //Add the default log appender if none exist
                if(logger.Appenders.Count == 0)
                {
                    var directoryName = "[Your directory name here. e.c. 'C:\ProgramData\AppName\Logs']";
    
                    //If the directory doesn't exist then create it
                    if(!Directory.Exists(directoryName))
                        Directory.CreateDirectory(directoryName);
    
                    var fileName = Path.Combine(directoryName, "[Your static file name here. e.c. 'AppName.log']");
    
                    //Create the rolling file appender
                    var appender = new log4net.Appender.RollingFileAppender();
                    appender.Name = "RollingFileAppender";
                    appender.File = fileName;
                    appender.StaticLogFileName = true;
                    appender.AppendToFile = false;
                    appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Size;
                    appender.MaxSizeRollBackups = 10;
                    appender.MaximumFileSize = "10MB";
                    appender.PreserveLogFileNameExtension = true;
    
                    //Configure the layout of the trace message write
                    var layout = new log4net.Layout.PatternLayout()
                    {
                        ConversionPattern = "%date{hh:mm:ss.fff} [%thread] %-5level - %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(loggerName);
                isConfigured = true;
    
                Info("Logging Configured at " + DateTime.Now.ToString("g"));
            }
    
            public static event EventHandler<ExceptionLoggedEventArgs> ExceptionLogged;
    
            public static void Debug(object message) { Configure(); iLog.Debug(message); }
            public static void Debug(object message, Exception exception) { Configure(); iLog.Debug(message, exception); }
    
            public static void Error(object message) { Configure(); iLog.Error(message); }
            public static void Error(object message, Exception exception) { Configure(); iLog.Error(message, exception); }
    
            public static void Fatal(object message) { Configure(); iLog.Fatal(message); }
            public static void Fatal(object message, Exception exception) { Configure(); iLog.Fatal(message, exception); }
    
            public static void Info(object message) { Configure(); iLog.Info(message); }
            public static void Info(object message, Exception exception) { Configure(); iLog.Info(message, exception); }
    
            public static void Warn(object message) { Configure(); iLog.Warn(message); }
            public static void Warn(object message, Exception exception) { Configure(); iLog.Warn(message, exception); }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

log4net in my project creates new log file every minute. I would like to
I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a class that runs something (a runnable) at regular
I would like to create an application which can learn to classify a sequence
I would like create URL rewrite rule that will set default document for my
I would like create a custom DataRow that will have -let's say- a propery
In my application I would like create a settings where user can be able
I would like create an inner shadow on my UIView on iPad like that
would like to create a function that generates graphs using ggplot. For the sake
I would like create my own collection that has all the attributes of python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.