Example #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));
        }
 /// <summary>
 /// Adds a sink that lets you push log messages to RabbitMQ
 /// Will be used when configuring via configuration file
 /// If you need to overrule the text formatter, you will need to supply it here as a separate parameter instead of supplying it via the RabbitMQSinkConfiguration instance
 /// which will not work when configuring via configuration file
 /// </summary>
 public static LoggerConfiguration RabbitMQ(
     this LoggerSinkConfiguration loggerConfiguration,
     RabbitMQClientConfiguration clientConfiguration, RabbitMQSinkConfiguration sinkConfiguration, ITextFormatter textFormatter = null)
 {
     if (textFormatter != null)
     {
         sinkConfiguration.TextFormatter = textFormatter;
     }
     return(RegisterSink(loggerConfiguration, clientConfiguration, sinkConfiguration));
 }
        /// <summary>
        /// Adds a sink that lets you push log messages to RabbitMQ
        /// </summary>
        public static LoggerConfiguration RabbitMQ(
            this LoggerSinkConfiguration loggerConfiguration,
            Action <RabbitMQClientConfiguration, RabbitMQSinkConfiguration> configure)
        {
            RabbitMQClientConfiguration clientConfiguration = new RabbitMQClientConfiguration();
            RabbitMQSinkConfiguration   sinkConfiguration   = new RabbitMQSinkConfiguration();

            configure(clientConfiguration, sinkConfiguration);

            return(RegisterSink(loggerConfiguration, clientConfiguration, sinkConfiguration));
        }
Example #4
0
        public static Serilog.ILogger AddCustomLogging(IServiceCollection services, IConfiguration configuration, string serviceName)
        {
            var logFormatter  = new ElasticsearchJsonFormatter();
            var rabbitCliente = new RabbitMQClientConfiguration();

            configuration.Bind("rabbitLog", rabbitCliente);
            rabbitCliente.SslOption = new RabbitMQ.Client.SslOption
            {
                Enabled = false,
            };

            rabbitCliente.DeliveryMode = RabbitMQDeliveryMode.Durable;

            var rabbitConfiguration = new RabbitMQSinkConfiguration
            {
                Period        = TimeSpan.FromDays(1),
                TextFormatter = logFormatter,
            };

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .Enrich.WithProperty("App Name", serviceName)
                         .Enrich.FromLogContext()
                         .Enrich.WithEnvironmentName()
                         .WriteTo.Logger(lc => lc.Filter.ByExcluding(Matching.WithProperty("AuditLog"))
                                         .WriteTo.Console(logFormatter)
                                         .WriteTo.RabbitMQ(rabbitCliente, rabbitConfiguration, logFormatter)
                                         )
                         .CreateLogger();

            var loggerFactory = new LoggerFactory();

            loggerFactory
            .AddSerilog();

            services.AddSingleton <ILoggerFactory>(loggerFactory);

            return(Log.Logger);
        }
        /// <summary>
        /// Adds a sink that lets you push log messages to RabbitMQ
        /// </summary>
        public static LoggerConfiguration RabbitMQ(
            this LoggerSinkConfiguration loggerConfiguration,
            Action <RabbitMQClientConfiguration, RabbitMQSinkConfiguration> configure)
        {
            RabbitMQClientConfiguration rabbitMQConfiguration     = new RabbitMQClientConfiguration();
            RabbitMQSinkConfiguration   rabbitMQSinkConfiguration = new RabbitMQSinkConfiguration();

            configure(rabbitMQConfiguration, rabbitMQSinkConfiguration);

            // guards
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException("loggerConfiguration");
            }
            if (rabbitMQConfiguration.Hostnames.Count == 0)
            {
                throw new ArgumentException("hostnames cannot be empty, specify at least one hostname", "hostnames");
            }
            if (string.IsNullOrEmpty(rabbitMQConfiguration.Username))
            {
                throw new ArgumentException("username cannot be 'null' or and empty string.");
            }
            if (rabbitMQConfiguration.Password == null)
            {
                throw new ArgumentException("password cannot be 'null'. Specify an empty string if password is empty.");
            }
            if (rabbitMQConfiguration.Port <= 0 || rabbitMQConfiguration.Port > 65535)
            {
                throw new ArgumentOutOfRangeException("port", "port must be in a valid range (1 and 65535)");
            }

            rabbitMQSinkConfiguration.BatchPostingLimit = (rabbitMQSinkConfiguration.BatchPostingLimit == default(int)) ? DefaultBatchPostingLimit : rabbitMQSinkConfiguration.BatchPostingLimit;
            rabbitMQSinkConfiguration.Period            = (rabbitMQSinkConfiguration.Period == default(TimeSpan)) ? DefaultPeriod : rabbitMQSinkConfiguration.Period;

            return
                (loggerConfiguration
                 .Sink(new RabbitMQSink(rabbitMQConfiguration, rabbitMQSinkConfiguration), rabbitMQSinkConfiguration.RestrictedToMinimumLevel));
        }
        static LoggerConfiguration RegisterSink(LoggerSinkConfiguration loggerConfiguration, RabbitMQClientConfiguration clientConfiguration, RabbitMQSinkConfiguration sinkConfiguration)
        {
            // guards
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException("loggerConfiguration");
            }
            if (clientConfiguration.Hostnames.Count == 0)
            {
                throw new ArgumentException("hostnames cannot be empty, specify at least one hostname", "hostnames");
            }
            if (string.IsNullOrEmpty(clientConfiguration.Username))
            {
                throw new ArgumentException("username cannot be 'null' or and empty string.");
            }
            if (clientConfiguration.Password == null)
            {
                throw new ArgumentException("password cannot be 'null'. Specify an empty string if password is empty.");
            }
            if (clientConfiguration.Port <= 0 || clientConfiguration.Port > 65535)
            {
                throw new ArgumentOutOfRangeException("port", "port must be in a valid range (1 and 65535)");
            }

            sinkConfiguration.BatchPostingLimit = (sinkConfiguration.BatchPostingLimit == default(int)) ? DefaultBatchPostingLimit : sinkConfiguration.BatchPostingLimit;
            sinkConfiguration.Period            = (sinkConfiguration.Period == default(TimeSpan)) ? DefaultPeriod : sinkConfiguration.Period;

            return
                (loggerConfiguration
                 .Sink(new RabbitMQSink(clientConfiguration, sinkConfiguration), sinkConfiguration.RestrictedToMinimumLevel, sinkConfiguration.LevelSwitch));
        }
Example #7
0
        private static RabbitMQSinkConfiguration RabbitMQSinkConfiguration()
        {
            var rabbitConfiguration = new RabbitMQSinkConfiguration();

            return(rabbitConfiguration);
        }
Example #8
0
 public RabbitMQSink(RabbitMQClientConfiguration configuration,
                     RabbitMQSinkConfiguration rabbitMQSinkConfiguration) : base(rabbitMQSinkConfiguration.BatchPostingLimit, rabbitMQSinkConfiguration.Period)
 {
     _formatter = rabbitMQSinkConfiguration.TextFormatter;
     _client    = new RabbitMQClient(configuration);
 }
 public static LoggerConfiguration RabbitMQ(
     this LoggerSinkConfiguration loggerConfiguration,
     RabbitMQClientConfiguration clientConfiguration, RabbitMQSinkConfiguration sinkConfiguration)
 {
     return(RegisterSink(loggerConfiguration, clientConfiguration, sinkConfiguration));
 }