0 Comments

If you want to create a customeventlog during setup, in a custom action. You can open a App.config file containing the log4net configuration and read the settings from that file at runtime.
You also can add appenders or remove appenders at runtime, but in this case I just wanted to read the configuration and create a custom eventlog:

       private string _productInstallationFolder = null;
       /// <summary>
       /// Returns C:\Program Files\MyCompany\MyProduct or C:\Program Files (x86)\MyCompany\MyProduct depending on the platform.
       /// </summary>
       public string ProductInstallationFolder
       {
           get
           {
               if (string.IsNullOrEmpty(_productInstallationFolder))
               {
                   var programFilesFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles);
                   _productInstallationFolder = Path.Combine(programFilesFolder, @"MyCompany\MyProduct");
               }
               return _productInstallationFolder;
           }
           set
           {
               _productInstallationFolder = value;
           }
       }
       public void CreateCustomEventLog()
       {
           XmlConfigurator.Configure(new FileInfo(string.Format("{0}.config", this.ProductInstallationFolder))); 
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; EventLog.CreateEventSource(eventLogAppender.ApplicationName, eventLogAppender.LogName); // Close application to allow the Windows eventlog service to refresh. // When applcation is restarted the first log event will create the log file. } } } } }

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.