private static void ApplyConfigForEventSource(LogConfiguration config, IEventLogger logger, EventSource source, LogSourceLevels levels) { if (config.HasFeature(LogConfiguration.Features.EventSourceSubscription)) { logger.SubscribeToEvents(source, levels.Level, levels.Keywords); } else if (config.HasFeature(LogConfiguration.Features.GuidSubscription)) { logger.SubscribeToEvents(source.Guid, levels.Level, levels.Keywords); } }
private static bool ParseLogSources(XmlNode xmlNode, LogConfiguration config) { bool clean = true; foreach (XmlNode source in xmlNode.SelectNodes(SourceTag)) { string sourceName = null; Guid sourceProvider = Guid.Empty; var sourceLevel = EventLevel.Informational; var sourceKeywords = (long)EventKeywords.None; foreach (XmlAttribute sourceAttribute in source.Attributes) { switch (sourceAttribute.Name.ToLower(CultureInfo.InvariantCulture)) { case SourceKeywordsAttribute: // Yes, really. The .NET integer TryParse methods will get PISSED if they see 0x in front of // hex digits. Dumb hack is dumb. string value = sourceAttribute.Value.Trim(); if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { value = value.Substring(2); } if (!long.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out sourceKeywords)) { InternalLogger.Write.InvalidConfiguration("invalid keywords value " + sourceAttribute.Value); clean = false; } break; case SourceMinSeverityAttribute: if (!Enum.TryParse(sourceAttribute.Value, true, out sourceLevel)) { InternalLogger.Write.InvalidConfiguration("invalid severity value " + sourceAttribute.Value); clean = false; } break; case SourceProviderIDAttribute: if (!Guid.TryParse(sourceAttribute.Value, out sourceProvider)) { InternalLogger.Write.InvalidConfiguration("invalid providerID GUID " + sourceAttribute.Value); clean = false; } break; case SourceProviderNameAttribute: sourceName = sourceAttribute.Value.Trim(); break; } } var levels = new LogSourceLevels(sourceLevel, (EventKeywords)sourceKeywords); if (sourceProvider != Guid.Empty) { config.GuidSources[sourceProvider] = levels; } else if (!string.IsNullOrEmpty(sourceName)) { config.NamedSources[sourceName] = levels; } else { InternalLogger.Write.InvalidConfiguration("source has neither name nor guid"); clean = false; } } return(clean); }
private static bool ParseLogSources(XmlNode xmlNode, LogConfiguration config) { bool clean = true; foreach (XmlNode source in xmlNode.SelectNodes(SourceTag)) { string sourceName = null; Guid sourceProvider = Guid.Empty; var sourceLevel = EventLevel.Informational; var sourceKeywords = (long)EventKeywords.None; foreach (XmlAttribute sourceAttribute in source.Attributes) { switch (sourceAttribute.Name.ToLower(CultureInfo.InvariantCulture)) { case SourceKeywordsAttribute: // Yes, really. The .NET integer TryParse methods will get PISSED if they see 0x in front of // hex digits. Dumb hack is dumb. string value = sourceAttribute.Value.Trim(); if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { value = value.Substring(2); } if (!long.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out sourceKeywords)) { InternalLogger.Write.InvalidConfiguration("invalid keywords value " + sourceAttribute.Value); clean = false; } break; case SourceMinSeverityAttribute: if (!Enum.TryParse(sourceAttribute.Value, true, out sourceLevel)) { InternalLogger.Write.InvalidConfiguration("invalid severity value " + sourceAttribute.Value); clean = false; } break; case SourceProviderIDAttribute: if (!Guid.TryParse(sourceAttribute.Value, out sourceProvider)) { InternalLogger.Write.InvalidConfiguration("invalid providerID GUID " + sourceAttribute.Value); clean = false; } break; case SourceProviderNameAttribute: sourceName = sourceAttribute.Value.Trim(); break; } } var levels = new LogSourceLevels(sourceLevel, (EventKeywords)sourceKeywords); if (sourceProvider != Guid.Empty) { config.GuidSources[sourceProvider] = levels; } else if (!string.IsNullOrEmpty(sourceName)) { config.NamedSources[sourceName] = levels; } else { InternalLogger.Write.InvalidConfiguration("source has neither name nor guid"); clean = false; } } return clean; }
private bool ApplyConfiguration() { var newConfig = new Dictionary <string, LogConfiguration>(StringComparer.OrdinalIgnoreCase); if (!ParseConfiguration(this.configurationFileData, newConfig) || !ParseConfiguration(this.configurationData, newConfig)) { return(false); } lock (this.loggersLock) { foreach (var logger in this.fileLoggers.Values) { logger.Dispose(); } foreach (var logger in this.networkLoggers.Values) { logger.Dispose(); } this.fileLoggers.Clear(); this.networkLoggers.Clear(); this.logConfigurations = newConfig; foreach (var kvp in this.logConfigurations) { string loggerName = kvp.Key; LogConfiguration loggerConfig = kvp.Value; IEventLogger logger; if (loggerConfig.FileType == LoggerType.Console) { // We re-create the console logger to clear its config (since we don't have a better way // to do that right now). this.CreateConsoleLogger(); logger = this.consoleLogger; } else if (loggerConfig.FileType == LoggerType.Network) { logger = this.CreateNetLogger(loggerName, loggerConfig.Hostname, loggerConfig.Port); } else { logger = this.CreateFileLogger(loggerConfig.FileType, loggerName, loggerConfig.Directory, loggerConfig.BufferSize, loggerConfig.RotationInterval, loggerConfig.FilenameTemplate, loggerConfig.TimestampLocal); } foreach (var f in loggerConfig.Filters) { logger.AddRegexFilter(f); } // Build a collection of all desired subscriptions so that we can subscribe in bulk at the end. // We do this because ordering may matter to specific types of loggers and they are best suited to // manage that internally. var subscriptions = new List <EventProviderSubscription>(); foreach (var ns in loggerConfig.NamedSources) { EventSourceInfo sourceInfo; string name = ns.Key; LogSourceLevels levels = ns.Value; if ((sourceInfo = GetEventSourceInfo(name)) != null) { subscriptions.Add(new EventProviderSubscription(sourceInfo.Source) { MinimumLevel = levels.Level, Keywords = levels.Keywords }); } } foreach (var gs in loggerConfig.GuidSources) { EventSourceInfo sourceInfo; Guid guid = gs.Key; LogSourceLevels levels = gs.Value; if (loggerConfig.HasFeature(LogConfiguration.Features.GuidSubscription)) { subscriptions.Add(new EventProviderSubscription(guid) { MinimumLevel = levels.Level, Keywords = levels.Keywords }); } else if (loggerConfig.HasFeature(LogConfiguration.Features.EventSourceSubscription) && (sourceInfo = GetEventSourceInfo(guid)) != null) { subscriptions.Add(new EventProviderSubscription(sourceInfo.Source) { MinimumLevel = levels.Level, Keywords = levels.Keywords }); } } logger.SubscribeToEvents(subscriptions); } } return(true); }