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); } } } } }
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) { foreach (var log in Configuration.Logs) { log.UpdateForEventSource(eventSource); } } }
/// <summary> /// Parses the <see cref="EventSource"/> class and returns the <see cref="EventSourceInfo"/> object /// </summary> /// <param name="eventSource">Event source to be parsed</param> /// <param name="friendlyName">Friendly name of the event source</param> /// <returns>event source info</returns> public static EventSourceInfo Parse(EventSource eventSource, string friendlyName) { if (string.IsNullOrEmpty(friendlyName)) { friendlyName = eventSource.Name; } var info = new EventSourceInfo { FriendlyName = friendlyName, }; foreach (var method in eventSource.GetType().GetMethods()) { var eventAttribute = method.GetCustomAttribute <EventAttribute>(); if (eventAttribute == null) { continue; } var id = eventAttribute.EventId; var name = method.Name; var parameters = method.GetParameters().Select(p => p.Name).ToArray(); info.events.Add(id, Tuple.Create(name, parameters)); } return(info); }
/// <summary> /// Adds the given event source to listening /// </summary> /// <param name="eventSourceName">Full name of the event source to be listened</param> /// <param name="level">Event level</param> /// <param name="shortName">Short and friendly name of the event source</param> /// <returns>True if the event source is listened successfully, false if otherwise</returns> public static bool AddEventSource(string eventSourceName, EventLevel level = EventLevel.Verbose, string shortName = null) { if (instance == null) { throw new InvalidOperationException(); } Contract.EndContractBlock(); var eventSource = EventSource.GetSources().FirstOrDefault(s => s.Name == eventSourceName); if (eventSource != null) { instance.listenedEventSources.Add(eventSource.Name, EventSourceInfo.Parse(eventSource, shortName)); instance.EnableEvents(eventSource, level); Trace($"Event source {eventSourceName} enabled."); return(true); } else { return(false); } }