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

  • SEARCH
  • Home
  • 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 7814957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:22:06+00:00 2026-06-02T05:22:06+00:00

Not sure whats going on here, but my logging code will not write to

  • 0

Not sure whats going on here, but my logging code will not write to the rollingFileAppender unless I call XmlConfigurator.Configure() on every call. I have turned on debugging in the following code and can confirm that when Configure is called just the one time in the constructor it does appear to be pulling in my configuration, but when Log() is actually called, nothing happens to the log file OR shows up in the debug window.
If I uncomment that call to Configure() in Log() and let it reconfigure on every call, then it works just fine, but I don’t think that is intended usage.

Bonus Points! – I notice that the very first call to log.Info() is not logging, but all subsequent ones in each run of the process are fine.

Thanks!

  public static class LogToFile
{
    public const string RollingFileAppenderName = "RollingFileLogger";

    static LogToFile()
    {
        log4net.Config.XmlConfigurator.Configure();
    }

    public static void Log(string fileNameBase, string message, string context)
    {
        if (fileNameBase == null) throw new ArgumentNullException("fileNameBase");
        if (message == null) throw new ArgumentNullException("message");
        if (context == null) throw new ArgumentNullException("context");

        //log4net.Config.XmlConfigurator.Configure();

        string fileName = string.Format("{0}_{1}.log", fileNameBase, context);
        string fullFileName = Path.Combine(Properties.Settings.Default.LogFilePath, fileName);

        if (!Directory.Exists(Properties.Settings.Default.LogFilePath)) Directory.CreateDirectory(Properties.Settings.Default.LogFilePath);

        LogicalThreadContext.Properties["LogName"] = string.Format(fullFileName);
        ILog log = LogManager.GetLogger(RollingFileAppenderName);

        log.Info(message);
    }
}

    <appender name="rollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="%property{LogName}" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="2MB" />
  <countDirection value="-1"/>
  <LockingModel value="log4net.Appender.FileAppender+MinimalLock"/>
  <staticLogFileName value="false" />
  <immediateFlush value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="AlertMessageHandler Message : %-25date [%thread] - %newline%message%newline" />
  </layout>
</appender>
  • 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-02T05:22:07+00:00Added an answer on June 2, 2026 at 5:22 am

    Actuall sgmoore’s advice led me to the solution. If I can just name the log file on the Appender object instead of messsing with the config file Property, then calling ActivateOptions works just fine. Here’s the working code. Thanks!

    public static class LogToFile
    {
        public const string RollingFileLoggerName = "RollingFileLogger";
    
        public const string RollingFileAppenderName = "rollingFileAppender";
    
        private static readonly ILog Logger = LogManager.GetLogger(RollingFileLoggerName);
    
        /// <summary>
        /// Logs to log4net rollingFileAppender
        /// </summary>
        /// <param name="fileNameBase">prefix of log filename</param>
        /// <param name="message">log4net message property</param>
        /// <param name="context">files grouped by context</param>
        public static void Log(string fileNameBase, string message, string context)
        {
            if (fileNameBase == null) throw new ArgumentNullException("fileNameBase");
            if (message == null) throw new ArgumentNullException("message");
            if (context == null) throw new ArgumentNullException("context");
    
            string fileName = string.Format("{0}_{1}.log", fileNameBase, context);
            string fullFileName = Path.Combine(Properties.Settings.Default.LogFilePath, fileName);
    
            if (!Directory.Exists(Properties.Settings.Default.LogFilePath)) Directory.CreateDirectory(Properties.Settings.Default.LogFilePath);
    
            ActivateAppenderOptions(string.Format(fullFileName));
    
            Logger.Info(message);
        }
    
        /// <summary>
        /// Update the appender in log4net after setting the log file path
        /// </summary>
        /// <param name="filename"></param>
        private static void ActivateAppenderOptions(string filename)
        {
            var hierarchy = (Hierarchy)LogManager.GetRepository();
            var appenders = hierarchy.GetAppenders();
            foreach (
                var rfa in appenders.OfType<RollingFileAppender>().Where(rfa => rfa.Name == RollingFileAppenderName))
            {
                rfa.File = filename;
                rfa.ActivateOptions();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok not sure whats going on the code does toggle class, but when you
I'm not sure what's going on here but here's a section I'm having problems
Not sure what's going on here, but my iPhone apps nav bar shows back
Not sure what exactly is going on here, but seems like in .NET 1.1
I'm not too sure what is going on here, but sometimes a particular file
Not sure what's going on here, but in my Rails 3 application, I type
Not really sure whats going on here, whether Im just being a fool or
I haven't used forms alot so Im not sure whats going on here. Im
I am not sure what is going on here but the rollover is not
Not quite sure what's going on here but it seems as though Microsoft always

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.