Ejemplo n.º 1
0
        public void WhenCreateException_ThenBuildShoulNotThrow()
        {
            var options = new GraylogSinkOptions();

            ExceptionMessageBuilder exceptionBuilder = new ExceptionMessageBuilder("localhost", options);

            Exception testExc = null;

            try
            {
                try
                {
                    throw new InvalidOperationException("Level One exception");
                }
                catch (Exception exc)
                {
                    throw new NotImplementedException("Nested Exception", exc);
                }
            }
            catch (Exception exc)
            {
                testExc = exc;
            }


            DateTimeOffset date     = DateTimeOffset.Now;
            LogEvent       logEvent = LogEventSource.GetExceptionLogEvent(date, testExc);

            JObject obj = exceptionBuilder.Build(logEvent);

            obj.Should().NotBeNull();
        }
        public void WhenGetSimpleEvent_ThenResult_ShouldBeExpected()
        {
            var options = new GraylogSinkOptions();
            var target  = new GelfMessageBuilder("localhost", options);

            var date = DateTimeOffset.Now;

            var expected = new
            {
                facility      = "GELF",
                full_message  = "abcdef\"zxc\"",
                host          = "localhost",
                level         = 2,
                short_message = "abcdef\"zxc\"",
                timestamp     = date.DateTime,
                version       = "1.1",
                _stringLevel  = "Information",
                _TestProp     = "\"zxc\"",
                _id_          = "\"asd\""
            };

            LogEvent logEvent = LogEventSource.GetSimpleLogEvent(date);

            string expectedString = JsonConvert.SerializeObject(expected, Newtonsoft.Json.Formatting.None);
            string actual         = target.Build(logEvent).ToString(Newtonsoft.Json.Formatting.None);
            //actual.ShouldBeEquivalentTo(expectedString);
        }
Ejemplo n.º 3
0
        public void WhenEmit_ThenSendData()
        {
            var gelfConverter = new Mock <IGelfConverter>();
            var transport     = new Mock <ITransport>();

            var options = new GraylogSinkOptions
            {
                GelfConverter     = gelfConverter.Object,
                TransportType     = TransportType.Udp,
                HostnameOrAddress = "localhost"
            };

            GraylogSink target = new GraylogSink(options);

            var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Fatal, null,
                                        new MessageTemplate("O_o", new List <MessageTemplateToken>()), new List <LogEventProperty>());

            var jObject = new JObject();

            transport.Setup(c => c.Send(jObject.ToString(Newtonsoft.Json.Formatting.None))).Returns(Task.CompletedTask);


            gelfConverter.Setup(c => c.GetGelfJson(logEvent)).Returns(jObject);

            target.Emit(logEvent);

            gelfConverter.VerifyAll();

            transport.Verify(c => c.Send(It.IsAny <string>()));
        }
Ejemplo n.º 4
0
        public PeriodicBatchingGraylogSink(GraylogSinkOptions options, int batchSizeLimit, TimeSpan period, int queueLimit) : base(batchSizeLimit, period, queueLimit)
        {
            ISinkComponentsBuilder sinkComponentsBuilder = new SinkComponentsBuilder(options);

            _transport = sinkComponentsBuilder.MakeTransport();
            _converter = sinkComponentsBuilder.MakeGelfConverter();
        }
        private static ITransport CreateHttpTransport(GraylogSinkOptions options)
        {
            var url           = new Uri($"{options.HostnameOrAddress}:{options.Port}/gelf");
            var httpTransport = new HttpTransport(url, options.HttpClientFactory);

            return(httpTransport);
        }
        public void TryComplexEvent()
        {
            var options = new GraylogSinkOptions();
            var target  = new GelfMessageBuilder("localhost", options);

            DateTimeOffset date = DateTimeOffset.Now;

            LogEvent logEvent = LogEventSource.GetComplexEvent(date);

            string actual = target.Build(logEvent).ToString(Newtonsoft.Json.Formatting.None);
        }
        public static ITransport FromOptions(GraylogSinkOptions options)
        {
            switch (options.TransportType)
            {
            case TransportType.Udp:
                return(CreateSyncUdpTransport(options));

            case TransportType.Http:
                return(CreateHttpTransport(options));

            default:
                throw new ArgumentOutOfRangeException(nameof(options), options.TransportType, null);
            }
        }
        private static ITransport CreateSyncUdpTransport(GraylogSinkOptions options)
        {
            var chunkConverter = new DataToChunkConverter(new ChunkSettings
            {
                MessageIdGeneratorType = options.MessageGeneratorType
            }, new MessageIdGeneratorResolver());

            var udpTransport = new UdpTransport(
                chunkConverter,
                options.HostnameOrAddress,
                options.Port,
                options.UdpClientFactory
                );

            return(udpTransport);
        }
        public void GetSimpleLogEvent_GraylogSinkOptionsContainsHost_ReturnsOptionsHost()
        {
            //arrange
            GraylogSinkOptions options = new GraylogSinkOptions()
            {
                Host = "my_host"
            };
            GelfMessageBuilder messageBuilder = new GelfMessageBuilder("localhost", options);
            DateTime           date           = DateTime.UtcNow;
            string             expectedHost   = "my_host";

            //act
            LogEvent logEvent   = LogEventSource.GetSimpleLogEvent(date);
            JObject  actual     = messageBuilder.Build(logEvent);
            string   actualHost = actual.Value <string>("host");

            //assert
            Assert.Equal(expectedHost, actualHost);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="GelfMessageBuilder"/> class.
 /// </summary>
 /// <param name="hostName">Name of the host.</param>
 /// <param name="options">The options.</param>
 public GelfMessageBuilder(string hostName = null, GraylogSinkOptions options = null)
 {
     Options                = options ?? new GraylogSinkOptions();
     this.hostName          = string.IsNullOrWhiteSpace(hostName) ? "localhost" : hostName;
     propertyNamingStrategy = options.PropertyNamingStrategy ?? new NoOpPropertyNamingStrategy();
 }
 public GelfMessageBuilder(string hostName, GraylogSinkOptions options)
 {
     _hostName = hostName;
     Options   = options;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GelfMessageBuilder"/> class.
 /// </summary>
 /// <param name="hostName">Name of the host.</param>
 /// <param name="options">The options.</param>
 public GelfMessageBuilder(string hostName, GraylogSinkOptions options)
 {
     _hostName = hostName;
     Options   = options;
     _propertyNamingStrategy = options.PropertyNamingStrategy;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ExceptionMessageBuilder"/> class.
 /// </summary>
 /// <param name="hostName">Name of the host.</param>
 /// <param name="options">The options.</param>
 public ExceptionMessageBuilder(string hostName, GraylogSinkOptions options) : base(hostName, options)
 {
 }
Ejemplo n.º 14
0
 public static LoggerConfiguration Graylog(this LoggerSinkConfiguration sinkConfiguration,
                                           GraylogSinkOptions options,
                                           LoggingLevelSwitch controllerSwitch)
 {
     return(sinkConfiguration.Sink(new GraylogSink(options), levelSwitch: controllerSwitch));
 }