public TraceLogEventSink(string ipAddress, int port, CancellationToken cancellationToken, TraceLogEventSinkConfigArgs traceLogEventSinkConfigArgs = null) : base() { queuedEvents = new FixedDictionary <LogEvent, LogEvent>(NumberExtensions.MB); this.Address = ipAddress; this.Port = port; this.CancellationToken = cancellationToken; this.traceLogEventSinkConfigArgs = traceLogEventSinkConfigArgs; this.ID = Guid.NewGuid().ToString(); this.Start(); cancellationToken.Register(() => { this.Stop(); }); }
public static LoggerConfiguration Trace(this LoggerSinkConfiguration sinkConfiguration, string ipAddress, int port, IConfigurationSection serilogConfig, CancellationToken cancellationToken) { TraceLogEventSink eventSink; var comparer = new ObjectHashCodeComparer <IConfiguration>(); var configurationTree = new ObjectTree <IConfiguration>(comparer); IConfigurationSection traceConfigurationSection; ObjectTreeItem <IConfiguration> treeItem; LinkedListNode <ObjectTreeItem <IConfiguration> > sibling; TraceLogEventSinkConfigArgs traceLogEventSinkConfigArgs = null; configurationTree.AddChild(serilogConfig); serilogConfig.GetDescendantsWithParent(s => s.GetChildren(), (parent, child) => { var parentItem = configurationTree.FindTreeItem(parent); parentItem.AddChild(child); }); traceConfigurationSection = configurationTree.GetDescendants().Select(d => d.InternalObject).OfType <IConfigurationSection>().SingleOrDefault(s => s.Path.RegexIsMatch(@"Serilog:WriteTo:(\d+:)?Name") && s.Value == "Trace"); treeItem = configurationTree.FindTreeItem(traceConfigurationSection); sibling = CompareExtensions.GetNonNull(treeItem.LinkedListNode.Next, treeItem.LinkedListNode.Previous); if (sibling != null && ((IConfigurationSection)sibling.Value.InternalObject).Key == "Args") { traceLogEventSinkConfigArgs = sibling.Value.InternalObject.Get <TraceLogEventSinkConfigArgs>(); } using (lockObject.Lock()) { if (eventSinks.Any(s => s.Address == ipAddress && s.Port == port)) { eventSink = eventSinks.Single(s => s.Address == ipAddress && s.Port == port); } else { eventSink = new TraceLogEventSink(ipAddress, port, cancellationToken, traceLogEventSinkConfigArgs); eventSinks.Add(eventSink); } } return(sinkConfiguration.Sink(eventSink)); }