Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change switchvalue on trace listener in runtime

Tags:

wcf

Hi Is it possible to change what levels of TraceEventType that a trace listener should log without restarting the WCF service? I'm letting the user configure what the trace should log, sends it to the service and then writes it to the config file. This solution requires the service to be restarted before the change takes effect...

Best Daniel

like image 618
Daniel Enetoft Avatar asked Feb 10 '26 20:02

Daniel Enetoft


1 Answers

It was easier than I thought. I keep track of the TraceSources and set their switchlevel when the user wants it 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 config file so the tracesource will get the same switchlevel next time the service is started.

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;
    }
}
like image 164
Daniel Enetoft Avatar answered Feb 18 '26 12:02

Daniel Enetoft