2010-10-28 56 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,你应该在重启过程中对用户产生最小的影响。

具有故障转移服务或负载平衡器会使用户看不到停机时间。

顺便说一下,我认为跟踪不应该被远程调用修改,但应该只能由管理员本地访问,出于安全原因。