修改loggingConfiguration Programmatic(企业库)

时间:2010-04-14 18:59:45

标签: configuration logging enterprise-library

我在m win应用程序和loggingConfiguration部分(企业库4.1)中有app.config。

我需要以编程方式执行此操作,

获取loggingConfiguration

中所有侦听器的列表

修改几个RollingFlatFileTraceListener的属性fileName =“。\ Trazas \ Excepciones.log”

修改AuthenticatingEmailTraceListener侦听器的几个属性,

任何建议,我都没有找到任何参考或样品

<listeners>

  <add name="Excepciones RollingFile Listener" fileName=".\Trazas\Excepciones.log" 
       formatter="Text Single Formatter" 
       footer="&lt;/Excepcion&gt;" 
       header="&lt;Excepcion&gt;"
       rollFileExistsBehavior="Overwrite" rollInterval="None" rollSizeKB="1500" timeStampPattern="yyyy-MM-dd" 
       listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />


  <add name="AuthEmailTraceListener"
            type="zzzz.Frk.Logging.AuthEmailTraceListener.AuthenticatingEmailTraceListener, zzzz.Frk.Logging.AuthEmailTraceListener"
            listenerDataType="zzzz.Frk.Logging.AuthEmailTraceListener.AuthenticatingEmailTraceListenerData, zzzz.Frk.Logging.AuthEmailTraceListener"
            formatter="Exception Formatter"
            traceOutputOptions="None"
            toAddress="xxxx@gmail.com"
            fromAddress="xxxx@gmail.com"
            subjectLineStarter=" Excepción detectada - "
            subjectLineEnder="incidencias"
            smtpServer="smtp.gmail.com"
            smtpPort="587" 
            authenticate="true"
            username="xxxxxxx@gmail.com"
            password="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            enableSsl="true"
       />

1 个答案:

答案 0 :(得分:2)

我不确定你是否可以通过编程方式完成。您必须将配置数据映射到实际的实现类(和属性)。另外,如果当前正在使用配置,则必须确保编程更改将覆盖配置。

在这个例子中,我读取了配置,更改了一些设置,复制了配置并将其写回配置文件。这很费力,因为许多属性都是只读的,因此需要创建新对象。编写配置应该可以工作,但我没有测试实际调用Enterprise Library之后(我真的不建议为生产应用程序执行它)。

// Open config file
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"MyApp.exe.config";

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

// Get EL log settings
LoggingSettings log = config.GetSection("loggingConfiguration") as LoggingSettings;
List<TraceListenerData> newListeners = new List<TraceListenerData>();

foreach(TraceListenerData listener in log.TraceListeners)
{
    // set new values for TraceListeners
    if (listener is FormattedEventLogTraceListenerData)
    {
        FormattedEventLogTraceListenerData oldData = listener as FormattedEventLogTraceListenerData;
        FormattedEventLogTraceListenerData data = 
            new FormattedEventLogTraceListenerData(oldData.Name, oldData.Source + "new", oldData.Log, oldData.MachineName, oldData.Formatter, oldData.TraceOutputOptions);

        newListeners.Add(data);
    }
    else if (listener is RollingFlatFileTraceListenerData)
    {
        RollingFlatFileTraceListenerData oldData = listener as RollingFlatFileTraceListenerData;
        RollingFlatFileTraceListenerData data =
            new RollingFlatFileTraceListenerData(oldData.Name, oldData.FileName + ".new", oldData.Header, oldData.Footer, oldData.RollSizeKB, oldData.TimeStampPattern, oldData.RollFileExistsBehavior, oldData.RollInterval, oldData.TraceOutputOptions, oldData.Formatter, oldData.Filter);

        newListeners.Add(data);
    }
}

// Replace the listeners
foreach (TraceListenerData traceListener in newListeners)
{
    log.TraceListeners.Remove(traceListener.Name);
    log.TraceListeners.Add(traceListener);
}

// Copy the LogSettings since when config.Sections.Remove() is called the original log object becomes "empty". 
LoggingSettings newLog = new LoggingSettings();

newLog.DefaultCategory = log.DefaultCategory;

foreach (var formatter in log.Formatters)
{
    newLog.Formatters.Add(formatter);
}

foreach (var filter in log.LogFilters)
{
    newLog.LogFilters.Add(filter);
}

foreach (var listener in log.TraceListeners)
{
    newLog.TraceListeners.Add(listener);
}

foreach (var source in log.TraceSources)
{
    newLog.TraceSources.Add(source);
}

newLog.LogWarningWhenNoCategoriesMatch = log.LogWarningWhenNoCategoriesMatch;
newLog.RevertImpersonation = log.RevertImpersonation;
newLog.SpecialTraceSources = log.SpecialTraceSources;
newLog.TracingEnabled = log.TracingEnabled;

// replace section
config.Sections.Remove("loggingConfiguration");
config.Sections.Add("loggingConfiguration", newLog);

// save and reload config
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("loggingConfiguration");

如果这不是你希望的100%,那么它会给你一些想法。