using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration; using System; using System.Collections; using System.Collections.Generic; using System.Configuration.Install; using System.Diagnostics; using System.Globalization; using System.IO; using System.Reflection; namespace Ploeh.Samples.LabInstaller { internal class LoggingApplicationBlockEventLogInstaller : Installer { internal LoggingApplicationBlockEventLogInstaller() { } public override void Install(IDictionary stateSaver) { this.AddEventLogInstallers(); base.Install(stateSaver); } public override void Uninstall(IDictionary savedState) { this.AddEventLogInstallers(); base.Uninstall(savedState); } private void AddEventLogInstallers() { Assembly currentAssembly = Assembly.GetExecutingAssembly(); string appBase = Path.GetDirectoryName(currentAssembly.Location); string configFile = string.Format(CultureInfo.InvariantCulture, "{0}.config", currentAssembly.Location); if (!File.Exists(configFile)) { this.Context.LogMessage(string.Format(CultureInfo.CurrentCulture, "Warning: The configuration file \"{0}\" doesn't exist. No event sources will be installed or uninstalled.", configFile)); return; } AppDomainSetup domainSetup = new AppDomainSetup(); domainSetup.ApplicationBase = appBase; domainSetup.ConfigurationFile = configFile; AppDomain targetAppDomain = AppDomain.CreateDomain( "TargetApplicationDomain", null, domainSetup); AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs e) { if (e.Name == currentAssembly.FullName) { return currentAssembly; } throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, "No assembly resolution logic was defined for {0}", e.Name)); }; LoggingConfigurationLoader configLoader = (LoggingConfigurationLoader)targetAppDomain.CreateInstanceAndUnwrap( typeof(LoggingConfigurationLoader).Assembly.FullName, typeof(LoggingConfigurationLoader).FullName); foreach (EventLogConfiguration elc in configLoader.EventLogs) { EventLogInstaller logInstaller = new EventLogInstaller(); logInstaller.Log = elc.Log; logInstaller.Source = elc.Source; this.Installers.Add(logInstaller); } AppDomain.Unload(targetAppDomain); } } internal class LoggingConfigurationLoader : MarshalByRefObject { private List eventLogs_; public LoggingConfigurationLoader() { this.eventLogs_ = new List(); IConfigurationSource configSource = ConfigurationSourceFactory.Create(); LoggingSettings loggingConfig = (LoggingSettings)configSource.GetSection( LoggingSettings.SectionName); foreach (TraceListenerData tld in loggingConfig.TraceListeners) { FormattedEventLogTraceListenerData feltld = tld as FormattedEventLogTraceListenerData; if (feltld != null) { EventLogConfiguration elc = new EventLogConfiguration(feltld.Log, feltld.Source); this.eventLogs_.Add(elc); } } } internal IList EventLogs { get { return this.eventLogs_; } } } [Serializable] internal class EventLogConfiguration { private string log_; private string source_; internal EventLogConfiguration(string log, string source) { this.log_ = log; this.source_ = source; } internal string Log { get { return this.log_; } } internal string Source { get { return this.source_; } } } }