2010-10-28 55 views
3

嗨 是否可以更改跟蹤偵聽器在不重新啓動WCF服務的情況下應記錄的TraceEventType的級別?我讓用戶配置跟蹤應記錄的內容,將其發送到服務,然後將其寫入配置文件。該解決方案需要更改生效之前,必須重新啓動該服務...在運行時更改跟蹤偵聽器上的switchvalue

最佳 丹尼爾

回答

3

這比我想象的要容易。我跟蹤TraceSources並在用戶希望更改時設置其開關級別。

private static void SetLogLevel(TraceSource traceSource, LogLevel logLevel) 
    { 
     switch (logLevel) 
     { 
      case LogLevel.Verbose: 
       traceSource.Switch.Level = SourceLevels.Verbose; 
       break; 
      case LogLevel.Information: 
       traceSource.Switch.Level = SourceLevels.Information; 
       break; 
      case LogLevel.Warning: 
       traceSource.Switch.Level = SourceLevels.Warning; 
       break; 
      case LogLevel.Error: 
       traceSource.Switch.Level = SourceLevels.Error; 
       break; 
      case LogLevel.Critical: 
       traceSource.Switch.Level = SourceLevels.Critical; 
       break; 
      default: 
       throw new ArgumentOutOfRangeException("logLevel"); 
     } 
    } 

我也在配置文件中寫入,這樣traceource在下次啓動服務時將獲得相同的switchlevel。

public TraceSourceConfiguration SetLogConfiguration(TraceSourceConfiguration traceSourceConfiguration) 
{ 
    lock (_lock) 
    { 
     Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     ConfigurationSection configurationSection = appConfig.GetSection(ConfigurationSectionName); 
     PropertyInformation traceSourceSection = 
      configurationSection.ElementInformation.Properties[TraceSourcesSectionName]; 
     if (traceSourceSection != null) 
     { 
      ConfigurationElementCollection traceSources = 
       (ConfigurationElementCollection)traceSourceSection.Value; 
      foreach (ConfigurationElement traceSource in traceSources) 
      { 
       string name = (string)traceSource.ElementInformation.Properties[LogLevelNameElementName].Value; 
       if (traceSourceConfiguration.Name == name) 
       { 
        traceSource.ElementInformation.Properties[LogLevelValueElementName].Value = 
         traceSourceConfiguration.SwitchValue; 
        appConfig.Save(); 
        ConfigurationManager.RefreshSection(ConfigurationSectionName); 
        TraceSourceHandler.SetTraceSourceSwitchLevel(traceSourceConfiguration.Name, traceSourceConfiguration.SwitchValue); 
        return traceSourceConfiguration; 
       } 

      } 
     } 
     return null; 
    } 
} 
0

據我知道這是不可能的。但是如果你的服務使用PerCall InstanceContextMode,你應該在重啓過程中對用戶產生最小的影響。

具有故障轉移服務或負載平衡器會使用戶看不到停機時間。

順便說一下,我認爲跟蹤不應該被遠程調用修改,但應該只能由管理員本地訪問,出於安全原因。