Change the value of switchvalue to listen to trace at runtime

Hi Can I change the TraceEventType levels that a trace listener should register without restarting the WCF service? I allow the user to configure the trace of the log, send it to the service, and then write to the configuration file. This solution requires restarting the service before the change takes effect ...

Best daniel

+3
source share
2 answers

It was easier than I thought. I track TraceSources and set their switch level when the user wants them to change.

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

I also write in the configuration file so that the next time the service starts, tracesource gets the same level of switching.

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;
    }
}
+3

, . PerCall InstanceContextMode, .

.

, , , .

0

Source: https://habr.com/ru/post/1771893/


All Articles