Пример #1
0
        public static LoggerConfiguration Nats(
            this LoggerSinkConfiguration loggerConfiguration,
            NatsConfiguration natsConfiguration,
            ITextFormatter formatter)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (natsConfiguration == null)
            {
                throw new ArgumentNullException(nameof(natsConfiguration));
            }

            // calls overloaded extension method
            return(loggerConfiguration.Nats(
                       natsConfiguration.ClientId,
                       natsConfiguration.Subject,
                       natsConfiguration.Host,
                       natsConfiguration.Port,
                       natsConfiguration.AutoRespondToPing,
                       natsConfiguration.AutoReconnectOnFailure,
                       natsConfiguration.Credentials,
                       natsConfiguration.PubFlushMode,
                       natsConfiguration.RequestTimeoutMs,
                       natsConfiguration.SocketOptions,
                       natsConfiguration.Verbose,
                       natsConfiguration.BatchPostingLimit,
                       (int)natsConfiguration.Period.TotalMilliseconds,
                       formatter));
        }
Пример #2
0
        public void IsMessageReceivedAfterLoggingIsDone()
        {
            var config = new NatsConfiguration
            {
                Host                   = "localhost",
                Port                   = 4222,
                ClientId               = "Serilog.Sinks.Nats.IntegrationTests.Publisher",
                Subject                = "IntegrationTest.TestSubject",
                RequestTimeoutMs       = 1000,
                AutoReconnectOnFailure = true,
                AutoRespondToPing      = true,
                BatchPostingLimit      = 1,
                Credentials            = new Credentials("test", "test"),
                Period                 = TimeSpan.FromMilliseconds(100),
                PubFlushMode           = PubFlushMode.Auto,
                SocketOptions          = new SocketOptions {
                    ConnectTimeoutMs = 1000, ReceiveBufferSize = 50000, ReceiveTimeoutMs = 5000, SendBufferSize = 50000, SendTimeoutMs = 5000
                },
                Verbose = false
            };

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .Enrich.FromLogContext()
                         .WriteTo.Nats(config, new JsonFormatter())
                         .CreateLogger();

            var cnInfo = new ConnectionInfo("localhost")
            {
                Credentials = new Credentials("test", "test"), RequestTimeoutMs = 30000
            };
            var client = new NatsClient("Serilog.Sinks.Nats.IntegrationTests.Subscriber", cnInfo);

            client.Connect();

            var foundMessage = false;

            client.SubWithHandlerAsync(config.Subject, msg => { foundMessage = true; }).ConfigureAwait(false);
            Log.Debug("Test message");

            Thread.Sleep(1000);
            client.Disconnect();

            Assert.True(foundMessage, "No log message received within timeout period");
        }
Пример #3
0
        private static LoggerConfiguration Nats(
            this LoggerSinkConfiguration loggerConfiguration,
            string clientId,
            string subject,
            string host,
            int port = 4222,
            bool autoRespondToPing      = true,
            bool autoReconnectOnFailure = true,
            Credentials credentials     = null,
            PubFlushMode pubFlushMode   = PubFlushMode.Auto,
            int requestTimeoutMs        = 10000,
            SocketOptions socketOptions = null,
            bool verbose             = false,
            int batchSizeLimit       = 50,
            int periodLimitInMs      = 100,
            ITextFormatter formatter = null)
        {
            // guards
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentOutOfRangeException(nameof(host), "host cannot be 'null' or and empty string.");
            }
            if (string.IsNullOrEmpty(clientId))
            {
                throw new ArgumentOutOfRangeException(nameof(clientId), "clientId cannot be 'null' or and empty string.");
            }
            if (string.IsNullOrEmpty(subject))
            {
                throw new ArgumentOutOfRangeException(nameof(subject), "subject cannot be 'null' or and empty string.");
            }
            if (port <= 0 || port > 65535)
            {
                throw new ArgumentOutOfRangeException(nameof(port), "port must be in a valid range (1 and 65535)");
            }

            // setup configuration
            var config = new NatsConfiguration
            {
                Subject  = subject,
                ClientId = clientId,
                Host     = host,
                Port     = port,
                AutoReconnectOnFailure = autoReconnectOnFailure,
                AutoRespondToPing      = autoRespondToPing,
                Credentials            = credentials,
                PubFlushMode           = pubFlushMode,
                RequestTimeoutMs       = requestTimeoutMs,
                SocketOptions          = socketOptions,
                Verbose           = verbose,
                BatchPostingLimit = batchSizeLimit,
                Period            = TimeSpan.FromMilliseconds(periodLimitInMs)
            };

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