Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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));
        }