代码之家  ›  专栏  ›  技术社区  ›  Daniel Enetoft

在运行时更改跟踪侦听器上的switchValue

wcf
  •  3
  • Daniel Enetoft  · 技术社区  · 15 年前

    您好! 是否可以在不重新启动WCF服务的情况下更改跟踪侦听器应记录的TraceEventType的级别?我让用户配置跟踪应该记录什么,将其发送到服务,然后将其写入配置文件。此解决方案要求在更改生效之前重新启动服务…

    最好的 丹尼尔

    2 回复  |  直到 14 年前
        1
  •  3
  •   abatishchev Karl Johan    14 年前

    比我想象的容易。我跟踪跟踪跟踪源,并在用户想要更改时设置它们的切换级别。

    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");
            }
        }
    

    我还写入了配置文件,这样下次启动服务时,跟踪源将获得相同的切换级别。

    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;
        }
    }
    
        2
  •  0
  •   Johann Blais    15 年前

    据我所知,这是不可能的。但是,如果您的服务使用percall instanceContextMode,那么在重新启动期间对用户的影响应该最小。

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

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

    推荐文章