You can read and change the Log4Net configuration at runtime, the following code, shows the eventlogname en eventlogsource for each eventlogappender defined, but if you change the eventLogAppender object properties the configuration will be changed.

            XmlConfigurator.Configure(new FileInfo(string.Format("{0}.config", @"C:\Temp\TestApplication.exe.config")));
            var repository = LogManager.GetRepository() as Hierarchy;
            if (repository != null)
            {
                var appenders = repository.GetAppenders();
                if (appenders != null)
                {
                    foreach (var appender in appenders)
                    {
                        if (appender is EventLogAppender)
                        {
                            var eventLogAppender = appender as EventLogAppender;
                            Console.WriteLine(string.Format("Eventlogname [{0}] and Eventlogsource [{1}] ", eventLogAppender.LogName, eventLogAppender.ApplicationName));
                        }
                    }
                }
            }

3 Comments

  1. Whilst the above code allows you to view and change the log and application settings, changing them has unexpected results.

    For example, if the config file initially sets to logName = “log1” and the applicationName = “app1”, events will be logged to the correct log and source. However, if you later change the source (ApplicationName) at runtime, all new events will be logged to the Application log, not to the “log1” log, even though the logName parameter still contains the value of “log1”.

    Although the source (Application) valuein the logged events is correct with the new value.

    MarkT
  2. Why does log4Net behave that way? Even after calling the ActivateOptions() method of the EventLogAppender all subsequent logs point to the “Application” log.

    nick
  3. Hi,

    I am new on log4net. I try to add IAppender at runtime lo Logger. I can create a RollinfileAppender by the way (create runtime without a config file). But I couldn’t log to db with (AdoNetAppender). I don’t take any error. How can I fix it ?

    aysegul

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.