public LoggerRelayEventSink(ILoggerRelay loggerRelay, CancellationToken cancellationToken, LoggerRelayEventSinkConfigArgs loggerRelayEventSinkConfigArgs = null) : base(ThreadPriority.Highest, 100) { bool mutexCreated; var outputTemplate = "{Timestamp:yyyy-MM-dd hh:mm:ss.fff} [{Level}] {SourceContext} {Message}"; DateTime.Now.ToDebugDateText(); this.Domain = loggerRelay.Domain; this.RootPath = Environment.ExpandEnvironmentVariables(loggerRelayEventSinkConfigArgs.RootPath); this.CancellationToken = cancellationToken; this.ID = Guid.NewGuid().ToString(); try { mutex = Mutex.OpenExisting(string.Format(@"Global\{0}+Mutex", this.Domain)); } catch { mutex = new Mutex(false, string.Format(@"Global\{0}+Mutex", this.Domain), out mutexCreated); } this.loggerRelayEventSinkConfigArgs = loggerRelayEventSinkConfigArgs; this.loggerRelay = loggerRelay; this.formatter = new MessageTemplateTextFormatter(outputTemplate); this.queuedEvents = new FixedDictionary <LoggerRelayEvent, LogEvent>(NumberExtensions.MB); if (loggerRelayEventSinkConfigArgs != null) { if (!loggerRelayEventSinkConfigArgs.Enabled) { return; } captureStack = loggerRelayEventSinkConfigArgs.CaptureStack.Where(c => c.Enabled).ToDictionary(c => c.Log, c => c.Assemblies.Where(a => a.Enabled).ToDictionary(a => a.Assembly, a => a)); } this.Start(); cancellationToken.Register(() => { this.Stopped = true; }); }
public static LoggerConfiguration Relay(this LoggerSinkConfiguration sinkConfiguration, ILoggerRelay loggerRelay, IConfigurationSection serilogConfig, CancellationToken cancellationToken) { LoggerRelayEventSink eventSink; var comparer = new ObjectHashCodeComparer <IConfiguration>(); var configurationTree = new ObjectTree <IConfiguration>(comparer); IConfigurationSection relayConfigurationSection; ObjectTreeItem <IConfiguration> treeItem; LinkedListNode <ObjectTreeItem <IConfiguration> > sibling; LoggerRelayEventSinkConfigArgs logRelayEventSinkConfigArgs = null; configurationTree.AddChild(serilogConfig); serilogConfig.GetDescendantsWithParent(s => s.GetChildren(), (parent, child) => { var parentItem = configurationTree.FindTreeItem(parent); parentItem.AddChild(child); }); relayConfigurationSection = configurationTree.GetDescendants().Select(d => d.InternalObject).OfType <IConfigurationSection>().SingleOrDefault(s => s.Path.RegexIsMatch(@"Serilog:WriteTo:(\d+:)?Name") && s.Value == "Relay"); if (relayConfigurationSection == null) { throw new ConfigurationException("Logger relay requires a configuration section with a RootPath member"); } else { treeItem = configurationTree.FindTreeItem(relayConfigurationSection); sibling = CompareExtensions.GetNonNull(treeItem.LinkedListNode.Next, treeItem.LinkedListNode.Previous); if (sibling != null && ((IConfigurationSection)sibling.Value.InternalObject).Key == "Args") { logRelayEventSinkConfigArgs = sibling.Value.InternalObject.Get <LoggerRelayEventSinkConfigArgs>(); } else { throw new ConfigurationException("Logger relay requires a configuration section with a RootPath member"); } loggerRelay.Initialize(logRelayEventSinkConfigArgs); } using (lockObject.Lock()) { if (eventSinks.Any(s => s.Domain == loggerRelay.Domain)) { eventSink = eventSinks.Single(s => s.Domain == loggerRelay.Domain); } else { eventSink = new LoggerRelayEventSink(loggerRelay, cancellationToken, logRelayEventSinkConfigArgs); eventSinks.Add(eventSink); } } return(sinkConfiguration.Sink(eventSink)); }