2012-12-19 100 views

回答

1

如果你知道在配置監聽器和類型的名稱,那麼你可以提取信息:以上

IConfigurationSource configSource = ConfigurationSourceFactory.Create(); 

var logSettings = configSource.GetSection(LoggingSettings.SectionName) as LoggingSettings; 

var listener = logSettings.TraceListeners 
    .SingleOrDefault(t => t.Name == "Rolling Flat File Trace Listener"); 

if (listener != null) 
{ 
    var fileListener = listener as RollingFlatFileTraceListenerData; 

    if (fileListener != null) 
    { 
     Console.WriteLine(fileListener.FileName); 
    } 
} 

是RollingFlatFileTraceListener。

或者,如果你只是想獲得支持FileName屬性,你可以做到這一切的跟蹤偵聽器中的文件名:

IConfigurationSource configSource = ConfigurationSourceFactory.Create(); 

var logSettings = configSource.GetSection(LoggingSettings.SectionName) as LoggingSettings; 

foreach (var listener in logSettings.TraceListeners) 
{ 
    PropertyInfo property = listener.GetType().GetProperty("FileName"); 

    if (property != null) 
    { 
     Console.WriteLine(
      string.Format("FileName for Trace Listener '{0}'={1}", 
       listener.Name, property.GetValue(listener, null))); 
    } 
}