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 shou开发者_如何学JAVAld 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
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;
}
}
As far as I know that is not possible. But if your service uses the PerCall InstanceContextMode, you should have minimal impact on users during the restart.
Having a fail-over service or a load-balancer would make the down-time invisible to the users.
By the way, I think the trace should not be modified by a remote call but should only be locally accessible to administrators for security reasons.
精彩评论