Beispiel #1
0
        /// <summary>
        /// Adds a sink that lets you push log messages to RabbitMQ
        /// Will be used when configuring via configuration file
        /// Is for backward-compatibility with previous version but gives possibility to use multiple hosts
        /// </summary>
        public static LoggerConfiguration RabbitMQ(
            this LoggerSinkConfiguration loggerConfiguration,
            string[] hostnames, string username, string password, string exchange = null, string exchangeType = null,
            RabbitMQDeliveryMode deliveryMode = RabbitMQDeliveryMode.NonDurable, string routeKey = null, int port = 0,
            string vHost    = null, ushort heartbeat = 0, IProtocol protocol = null, int batchPostingLimit = 0,
            TimeSpan period = default, ITextFormatter formatter = null
            )
        {
            RabbitMQClientConfiguration clientConfiguration = new RabbitMQClientConfiguration
            {
                Username     = username,
                Password     = password,
                Exchange     = exchange,
                ExchangeType = exchangeType,
                DeliveryMode = deliveryMode,
                RouteKey     = routeKey,
                Port         = port,
                VHost        = vHost,
                Heartbeat    = heartbeat,
                Protocol     = protocol
            };

            foreach (string hostname in hostnames)
            {
                clientConfiguration.Hostnames.Add(hostname);
            }

            RabbitMQSinkConfiguration sinkConfiguration = new RabbitMQSinkConfiguration
            {
                BatchPostingLimit = batchPostingLimit, Period = period, TextFormatter = formatter
            };

            return(RegisterSink(loggerConfiguration, clientConfiguration, sinkConfiguration));
        }
Beispiel #2
0
 /// <summary>
 /// Adds a sink that lets you push log messages to RabbitMQ
 /// Will be used when configuring via configuration file
 /// Is for backward-compatibility with previous version
 /// </summary>
 public static LoggerConfiguration RabbitMQ(
     this LoggerSinkConfiguration loggerConfiguration,
     string hostname, string username, string password, string exchange = null, string exchangeType = null,
     RabbitMQDeliveryMode deliveryMode = RabbitMQDeliveryMode.NonDurable, string routeKey           = null, int port = 0,
     string vHost    = null, ushort heartbeat = 0, IProtocol protocol = null, int batchPostingLimit = 0,
     TimeSpan period = default(TimeSpan), ITextFormatter formatter = null, IFormatProvider formatProvider = null
     )
 {
     return(loggerConfiguration.RabbitMQ(new string[] { hostname }, username, password, exchange, exchangeType,
                                         deliveryMode, routeKey, port, vHost, heartbeat, protocol, batchPostingLimit, period, formatter));
 }
        /// <summary>
        /// Configures Serilog logger configuration with RabbitMQ
        /// </summary>
        public static LoggerConfiguration RabbitMQ(
            this LoggerSinkConfiguration loggerConfiguration,
            string hostname,
            string username,
            string password,
            string exchange,
            string exchangeType,
            RabbitMQDeliveryMode deliveryMode,
            string routeKey,
            int port,
            string vHost,
            ushort heartbeat,
            IProtocol protocol,
            int batchPostingLimit,
            TimeSpan period,
            ITextFormatter formatter,
            IFormatProvider formatProvider = null)
        {
            // guards
            if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration");
            if (string.IsNullOrEmpty(hostname)) throw new ArgumentException("hostname cannot be 'null'. Enter a valid hostname.");
            if (string.IsNullOrEmpty(username)) throw new ArgumentException("username cannot be 'null' or and empty string.");
            if (password == null) throw new ArgumentException("password cannot be 'null'. Specify an empty string if password is empty.");
            if (port <= 0 || port > 65535) throw new ArgumentOutOfRangeException("port", "port must be in a valid range (1 and 65535)");

            // setup configuration
            var config = new RabbitMQConfiguration
            {
                Hostname = hostname,
                Username = username,
                Password = password,
                Exchange = exchange ?? string.Empty,
                ExchangeType = exchangeType ?? string.Empty,
                DeliveryMode = deliveryMode,
                RouteKey = routeKey ?? string.Empty,
                Port = port,
                VHost = vHost ?? string.Empty,
                Protocol = protocol ?? Protocols.DefaultProtocol,
                Heartbeat = heartbeat,
                BatchPostingLimit = batchPostingLimit == default(int) ? DefaultBatchPostingLimit : batchPostingLimit,
                Period = period == default(TimeSpan) ? DefaultPeriod : period
            };

            return
                loggerConfiguration
                    .Sink(new RabbitMQSink(config, formatter, formatProvider));
        }
        /// <summary>
        /// Configures Serilog logger configuration with RabbitMQ
        /// </summary>
        public static LoggerConfiguration RabbitMQ(
            this LoggerSinkConfiguration loggerConfiguration,
            string hostname,
            string username,
            string password,
            string exchange,
            string exchangeType,
            string queue,
            RabbitMQDeliveryMode deliveryMode,
            string routeKey,
            int port,
            string vHost,
            ushort heartbeat,
            IProtocol protocol,
            int batchPostingLimit,
            TimeSpan period,
            ITextFormatter formatter,
            IFormatProvider formatProvider = null)
        {
            // guards
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException("loggerConfiguration");
            }
            if (string.IsNullOrEmpty(hostname))
            {
                throw new ArgumentException("hostname cannot be 'null'. Enter a valid hostname.");
            }
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("username cannot be 'null' or and empty string.");
            }
            if (password == null)
            {
                throw new ArgumentException("password cannot be 'null'. Specify an empty string if password is empty.");
            }
            if (string.IsNullOrEmpty(queue))
            {
                throw new ArgumentException("queue cannot be 'null'. Specify a valid queue.");
            }
            if (port <= 0 || port > 65535)
            {
                throw new ArgumentOutOfRangeException("port", "port must be in a valid range (1 and 65535)");
            }

            // setup configuration
            var config = new RabbitMQConfiguration
            {
                Hostname          = hostname,
                Username          = username,
                Password          = password,
                Exchange          = exchange ?? string.Empty,
                ExchangeType      = exchangeType ?? string.Empty,
                Queue             = queue,
                DeliveryMode      = deliveryMode,
                RouteKey          = routeKey ?? string.Empty,
                Port              = port,
                VHost             = vHost ?? string.Empty,
                Protocol          = protocol ?? Protocols.DefaultProtocol,
                Heartbeat         = heartbeat,
                BatchPostingLimit = batchPostingLimit == default(int) ? DefaultBatchPostingLimit : batchPostingLimit,
                Period            = period == default(TimeSpan) ? DefaultPeriod : period
            };

            return
                (loggerConfiguration
                 .Sink(new RabbitMQSink(config, formatter, formatProvider)));
        }