示例#1
0
 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);
     }
 }
示例#2
0
        protected override void OnEventSourceCreated(EventSource eventSource)
        {
            // There is a possible race where we can get notified of the creation of our own internal logger during
            // construction of the log manager. The LogManager constructor helps handle this by explicitly calling
            // this function once it is safe to do so.
            if (eventSource is InternalLogger && InternalLogger.Write == null)
            {
                return;
            }

            lock (this.eventSourceInfosLock)
            {
                if (!this.eventSourceInfos.ContainsKey(eventSource))
                {
                    var updatedData = new EventSourceInfo(eventSource);
                    this.eventSourceInfos[eventSource] = updatedData;
                    InternalLogger.Write.NewEventSource(eventSource.Name, eventSource.Guid);
                }
                else
                {
                    return; // we should have already done the work for this source.
                }
            }
            lock (this.loggersLock)
            {
                if (this.logConfigurations != null)
                {
                    foreach (var kvp in this.logConfigurations)
                    {
                        LogConfiguration config = kvp.Value;
                        // We need to update our loggers any time a config shows up where they had a dependency
                        // that probably wasn't resolved. This will be the case either when it was a named source
                        // or it was a GUID source on a type that can't directly subscribe to GUIDs (i.e. not an ETW
                        // trace session)
                        IEventLogger logger = null;
                        switch (config.FileType)
                        {
                        case LoggerType.Console:
                            logger = this.consoleLogger;
                            break;

                        case LoggerType.Network:
                            logger = this.GetNamedNetworkLogger(kvp.Key);
                            break;

                        default:
                            logger = this.GetNamedFileLogger(kvp.Key).Logger;
                            break;
                        }
                        LogSourceLevels levels;
                        if (config.NamedSources.TryGetValue(eventSource.Name, out levels) ||
                            (!config.HasFeature(LogConfiguration.Features.GuidSubscription) &&
                             config.GuidSources.TryGetValue(eventSource.Guid, out levels)))
                        {
                            ApplyConfigForEventSource(config, logger, eventSource, levels);
                        }
                    }
                }
            }
        }
示例#3
0
        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);
        }