示例#1
0
        public void ShouldCreateGelfJsonCorrectly()
        {
            // arrange
            var timestamp = DateTime.Now;
            var logEvent  = new LogEventInfo
            {
                Message    = "Test Log Message",
                Level      = LogLevel.Info,
                TimeStamp  = timestamp,
                LoggerName = "GelfConverterTestLogger",
            };

            logEvent.Properties.Add("customproperty1", "customvalue1");
            logEvent.Properties.Add("customproperty2", "customvalue2");

            // act
            var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility", false);

            // assert
            jsonObject.Should().NotBeNull();
            jsonObject.Value <string>("version").Should().Be("1.1");
            jsonObject.Value <string>("host").Should().Be(Dns.GetHostName());
            jsonObject.Value <string>("short_message").Should().Be("Test Log Message");
            jsonObject.Value <string>("full_message").Should().Be("Test Log Message");
            jsonObject.Value <double>("timestamp").Should().Be(timestamp.ToUnixTimestamp());
            jsonObject.Value <int>("level").Should().Be(5);

            jsonObject.Value <string>("_facility").Should().Be("TestFacility");
            jsonObject.Value <string>("_customproperty1").Should().Be("customvalue1");
            jsonObject.Value <string>("_customproperty2").Should().Be("customvalue2");
            jsonObject.Value <string>("_LoggerName").Should().Be("GelfConverterTestLogger");

            // make sure that there are no other junk in there
            jsonObject.Should().HaveCount(10);
        }
示例#2
0
        public void ShouldHandleExceptionsCorrectly()
        {
            // arrange
            var options  = Mock.Of <IConvertOptions>(o => o.Facility == "TestFacility");
            var logEvent = new LogEventInfo
            {
                Message   = "Test Message",
                Exception = new DivideByZeroException("div by 0"),
            };

            // act
            var jsonObject = new GelfConverter().GetGelfJson(logEvent, options);

            // assert
            jsonObject.Should().NotBeNull();
            jsonObject.Value <string>("short_message").Should().Be("Test Message");
            jsonObject.Value <string>("full_message").Should().Be("Test Message");
            jsonObject.Value <int>("level").Should().Be(3);
            jsonObject.Value <string>("_facility").Should().Be(options.Facility);
            jsonObject.Value <string>("_ExceptionSource").Should().Be(null);
            jsonObject.Value <string>("_Exception.0.Type").Should().Be(typeof(DivideByZeroException).FullName);
            jsonObject.Value <string>("_Exception.0.Message").Should().Be("div by 0");
            jsonObject.Value <string>("_Exception.0.StackTrace").Should().Be(null);
            jsonObject.Value <string>("_LoggerName").Should().Be(null);
        }
示例#3
0
            public void ShouldHandleLongMessageCorrectly()
            {
                var logEvent = new LogEventInfo
                {
                    //The first 300 chars of lorem ipsum...
                    Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus interdum est in est cursus vitae pellentesque felis lobortis. Donec a orci quis ante viverra eleifend ac et quam. Donec imperdiet libero ut justo tincidunt non tristique mauris gravida. Fusce sapien eros, tincidunt a placerat nullam."
                };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual(250, jsonObject.Value <string>("short_message").Length);
                Assert.AreEqual(300, jsonObject.Value <string>("full_message").Length);
            }
示例#4
0
            public void ShouldHandle10NestedExceptionCorrectly()
            {
                var nestedException = new Exception("Inner Exception Detail - 10");

                for (int i = 9; i > 0; i--)
                {
                    var nextException = new Exception("Inner Exception Detail - " + i.ToString(), nestedException);
                    nestedException = nextException;
                }
                var outerException = new Exception("Outer Exception Detail", nestedException);

                var logEvent = new LogEventInfo
                {
                    Message   = "Test Message",
                    Exception = outerException
                };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual("Test Message", jsonObject.Value <string>("short_message"));
                Assert.AreEqual("Test Message", jsonObject.Value <string>("full_message"));
                Assert.AreEqual(3, jsonObject.Value <int>("level"));
                Assert.AreEqual("TestFacility", jsonObject.Value <string>("facility"));
                Assert.AreEqual(null, jsonObject.Value <string>("_ExceptionSource"));
                const string expectedExceptionDetail =
                    "Outer Exception Detail - Inner Exception Detail - 1 - Inner Exception Detail - 2 - Inner Exception Detail - 3 - Inner Exception Detail - 4 - Inner Exception Detail - 5 - Inner Exception Detail - 6 - Inner Exception Detail - 7 - Inner Exception Detail - 8 - Inner Exception Detail - 9 - Inner Exception Detail - 10";

                Assert.AreEqual(expectedExceptionDetail, jsonObject.Value <string>("_ExceptionMessage"));
                Assert.AreEqual(null, jsonObject.Value <string>("_StackTrace"));
                Assert.AreEqual(null, jsonObject.Value <string>("_LoggerName"));
            }
示例#5
0
        public void ShouldHandleLongMessageCorrectly()
        {
            // arrange
            var logEvent = new LogEventInfo
            {
                // The first 300 chars of lorem ipsum...
                Message = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus interdum est in est cursus vitae pellentesque felis lobortis. Donec a orci quis ante viverra eleifend ac et quam. Donec imperdiet libero ut justo tincidunt non tristique mauris gravida. Fusce sapien eros, tincidunt a placerat nullam.",
            };

            // act
            var jsonObject = new GelfConverter().GetGelfJson(logEvent, Mock.Of <IConvertOptions>());

            // assert
            jsonObject.Should().NotBeNull();
            jsonObject.Value <string>("short_message").Length.Should().Be(250);
            jsonObject.Value <string>("full_message").Length.Should().Be(300);
        }
示例#6
0
        public void ShouldHandleNestedExceptionCorrectly()
        {
            // arrange
            var logEvent = new LogEventInfo
            {
                Message   = "Test Message",
                Exception = new AggregateException("div by 0", new Exception("Nested exception")),
            };

            // act
            var jsonObject = new GelfConverter().GetGelfJson(logEvent, Mock.Of <IConvertOptions>());

            // assert
            jsonObject.Should().NotBeNull();
            jsonObject.Value <string>("_Exception.1.Type").Should().Be(typeof(Exception).FullName);
            jsonObject.Value <string>("_Exception.1.Message").Should().Be("Nested exception");
        }
示例#7
0
            public void ShouldSetDefaultFacility(string facility)
            {
                var logEvent = new LogEventInfo {
                    Message = "Test"
                };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, facility);

                Assert.AreEqual("GELF", jsonObject.Value <string>("facility"));
            }
示例#8
0
        public void ShouldRedactCreditCardNumber()
        {
            var logEvent = new LogEventInfo {
                Message = "Test 4111111111111111 Test"
            };

            logEvent.Properties.Add("Id", "not_important");

            var redactInfos = new List <RedactInfo>
            {
                new RedactInfo
                {
                    Pattern     = "4[0-9]{12}(?:[0-9]{3})?",
                    Replacement = "REDACTED"
                }
            };

            var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility", "DEV", redactInfos);

            Assert.Equal("Test REDACTED Test", jsonObject.Value <string>("short_message"));
            Assert.Equal("Test REDACTED Test", jsonObject.Value <string>("full_message"));
        }
示例#9
0
            public void ShouldHandlePropertyCalledIdProperly()
            {
                var logEvent = new LogEventInfo {
                    Message = "Test"
                };

                logEvent.Properties.Add("Id", "not_important");

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.IsNull(jsonObject["_id"]);
                Assert.AreEqual("not_important", jsonObject.Value <string>("_id_"));
            }
示例#10
0
        public void ShouldHandleExceptionsCorrectly()
        {
            var logEvent = new LogEventInfo
            {
                Message   = "Test Message",
                Exception = new DivideByZeroException("div by 0")
            };

            var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility", "DEV", new List <RedactInfo>());

            Assert.NotNull(jsonObject);
            Assert.Equal("Test Message", jsonObject.Value <string>("short_message"));
            Assert.Equal("Test Message", jsonObject.Value <string>("full_message"));
            Assert.Equal(3, jsonObject.Value <int>("level"));
            Assert.Equal("TestFacility", jsonObject.Value <string>("_application"));
            Assert.Equal("DEV", jsonObject.Value <string>("_environment"));
            Assert.Equal(null, jsonObject.Value <string>("_ExceptionSource"));
            Assert.Equal("div by 0", jsonObject.Value <string>("_ExceptionMessage"));
            Assert.Equal(null, jsonObject.Value <string>("_StackTrace"));
            Assert.Equal(null, jsonObject.Value <string>("_LoggerName"));
        }
示例#11
0
            public void ShouldHandleExceptionsCorrectly()
            {
                var logEvent = new LogEventInfo
                {
                    Message   = "Test Message",
                    Exception = new DivideByZeroException("div by 0")
                };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, new SimpleLayout(), "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual("Test Message", jsonObject.Value <string>("short_message"));
                Assert.AreEqual("Test Message", jsonObject.Value <string>("full_message"));
                Assert.AreEqual(3, jsonObject.Value <int>("level"));
                Assert.AreEqual("TestFacility", jsonObject.Value <string>("facility"));
                Assert.AreEqual(null, jsonObject.Value <string>("_ExceptionSource"));
                Assert.AreEqual("div by 0", jsonObject.Value <string>("_ExceptionMessage"));
                Assert.AreEqual(null, jsonObject.Value <string>("_StackTrace"));
                Assert.AreEqual(null, jsonObject.Value <string>("_LoggerName"));
            }
            public void ShouldHandleExceptionsCorrectly()
            {
                var logEvent = new LogEventInfo
                                   {
                                       Message = "Test Message",
                                       Exception = new DivideByZeroException("div by 0")
                                   };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual("Test Message", jsonObject.Value<string>("short_message"));
                Assert.AreEqual("Test Message", jsonObject.Value<string>("full_message"));
                Assert.AreEqual(3, jsonObject.Value<int>("level"));
                Assert.AreEqual("TestFacility", jsonObject.Value<string>("facility"));
                Assert.AreEqual(null, jsonObject.Value<string>("_ExceptionSource"));
                Assert.AreEqual("div by 0", jsonObject.Value<string>("_ExceptionMessage"));
                Assert.AreEqual(null, jsonObject.Value<string>("_StackTrace"));
                Assert.AreEqual(null, jsonObject.Value<string>("_LoggerName"));
            }
示例#13
0
            public void ShouldHandleNestedExceptionCorrectly()
            {
                var logEvent = new LogEventInfo
                {
                    Message   = "Test Message",
                    Exception = new Exception("Outer Exception Detail", new Exception("Inner Exception Detail"))
                };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual("Test Message", jsonObject.Value <string>("short_message"));
                Assert.AreEqual("Test Message", jsonObject.Value <string>("full_message"));
                Assert.AreEqual(3, jsonObject.Value <int>("level"));
                Assert.AreEqual("TestFacility", jsonObject.Value <string>("facility"));
                Assert.AreEqual(null, jsonObject.Value <string>("_ExceptionSource"));
                Assert.AreEqual("Outer Exception Detail - Inner Exception Detail", jsonObject.Value <string>("_ExceptionMessage"));
                Assert.AreEqual(null, jsonObject.Value <string>("_StackTrace"));
                Assert.AreEqual(null, jsonObject.Value <string>("_LoggerName"));
            }
示例#14
0
        public void ShouldIncludeMdlcProperties()
        {
            // arrange
            var logEvent = new LogEventInfo
            {
                Message = "Message",
            };

            // act
            JObject jsonObject;

            using (MappedDiagnosticsLogicalContext.SetScoped("mdlcItem", "value1"))
            {
                jsonObject = new GelfConverter().GetGelfJson(logEvent, Mock.Of <IConvertOptions>(o => o.IncludeMdlcProperties));
            }

            // assert
            jsonObject.Should().NotBeNull();
            jsonObject.Value <string>("_mdlcItem").Should().Be("value1");
        }
            public void ShouldHandleNestedExceptionCorrectly()
            {
                var logEvent = new LogEventInfo
                {
                    Message = "Test Message",
                    Exception = new Exception("Outer Exception Detail", new Exception("Inner Exception Detail"))
                };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual("Test Message", jsonObject.Value<string>("short_message"));
                Assert.AreEqual("Test Message", jsonObject.Value<string>("full_message"));
                Assert.AreEqual(3, jsonObject.Value<int>("level"));
                Assert.AreEqual("TestFacility", jsonObject.Value<string>("facility"));
                Assert.AreEqual(null, jsonObject.Value<string>("_ExceptionSource"));
                Assert.AreEqual("Outer Exception Detail - Inner Exception Detail", jsonObject.Value<string>("_ExceptionMessage"));
                Assert.AreEqual(null, jsonObject.Value<string>("_StackTrace"));
                Assert.AreEqual(null, jsonObject.Value<string>("_LoggerName"));
            }
示例#16
0
            public void ShouldCreateGelfJsonCorrectly()
            {
                var timestamp = DateTime.Now;
                var logEvent  = new LogEventInfo
                {
                    Message    = "Test Log Message",
                    Level      = LogLevel.Info,
                    TimeStamp  = timestamp,
                    LoggerName = "GelfConverterTestLogger"
                };

                logEvent.Properties.Add("customproperty1", "customvalue1");
                logEvent.Properties.Add("customproperty2", "customvalue2");
                logEvent.Properties.Add("custompropertyint", 199);
                logEvent.Properties.Add("custompropertyarray", new[] { 1, 2, 3 });

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual("1.0", jsonObject.Value <string>("version"));
                Assert.AreEqual(Dns.GetHostName(), jsonObject.Value <string>("host"));
                Assert.AreEqual("Test Log Message", jsonObject.Value <string>("short_message"));
                Assert.AreEqual("Test Log Message", jsonObject.Value <string>("full_message"));
                Assert.AreEqual(((DateTimeOffset)timestamp).ToUnixTimeSeconds(), jsonObject.Value <long>("timestamp"));
                Assert.AreEqual(6, jsonObject.Value <int>("level"));
                Assert.AreEqual("TestFacility", jsonObject.Value <string>("facility"));
                Assert.AreEqual("", jsonObject.Value <string>("file"));
                Assert.AreEqual("", jsonObject.Value <string>("line"));

                Assert.AreEqual("customvalue1", jsonObject.Value <string>("_customproperty1"));
                Assert.AreEqual("customvalue2", jsonObject.Value <string>("_customproperty2"));
                Assert.AreEqual(199, jsonObject.Value <int>("_custompropertyint"));
                Assert.AreEqual(new[] { 1, 2, 3 }, jsonObject["_custompropertyarray"].ToObject <int[]>());
                Assert.AreEqual("GelfConverterTestLogger", jsonObject.Value <string>("_LoggerName"));

                //make sure that there are no other junk in there
                Assert.AreEqual(14, jsonObject.Count);
            }
            public void ShouldHandlePropertyCalledIdProperly()
            {
                var logEvent = new LogEventInfo { Message = "Test" };
                logEvent.Properties.Add("Id", "not_important");

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.IsNull(jsonObject["_id"]);
                Assert.AreEqual("not_important", jsonObject.Value<string>("_id_"));
            }
示例#18
0
        public void ShouldCreateGelfJsonCorrectly()
        {
            var timestamp = DateTime.Now;
            var logEvent  = new LogEventInfo
            {
                Message    = "Test Log Message",
                Level      = LogLevel.Info,
                TimeStamp  = timestamp,
                LoggerName = "GelfConverterTestLogger"
            };

            logEvent.Properties.Add("customproperty1", "customvalue1");
            logEvent.Properties.Add("customproperty2", "customvalue2");

            var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility", "DEV", new List <RedactInfo>());

            Assert.NotNull(jsonObject);
            Assert.Equal("1.1", jsonObject.Value <string>("version"));
            Assert.Equal(Dns.GetHostName().ToUpper(), jsonObject.Value <string>("host"));
            Assert.Equal("Test Log Message", jsonObject.Value <string>("short_message"));
            Assert.Equal("Test Log Message", jsonObject.Value <string>("full_message"));
            Assert.Equal(timestamp, jsonObject.Value <DateTime>("timestamp"));
            Assert.Equal(6, jsonObject.Value <int>("level"));
            Assert.Equal("TestFacility", jsonObject.Value <string>("_application"));
            Assert.Equal("DEV", jsonObject.Value <string>("_environment"));

            Assert.Equal("customvalue1", jsonObject.Value <string>("_customproperty1"));
            Assert.Equal("customvalue2", jsonObject.Value <string>("_customproperty2"));
            Assert.Equal("GelfConverterTestLogger", jsonObject.Value <string>("_LoggerName"));
            Assert.Equal("Info", jsonObject.Value <string>("_LogLevelName"));

            //make sure that there are no other junk in there
            Assert.Equal(12, jsonObject.Count);
        }
            public void ShouldHandle10NestedExceptionCorrectly()
            {
                var nestedException = new Exception("Inner Exception Detail - 10");
                for (int i = 9; i > 0; i--)
                {
                    var nextException = new Exception("Inner Exception Detail - " + i.ToString(), nestedException);
                    nestedException = nextException;
                }
                var outerException = new Exception("Outer Exception Detail", nestedException);

                var logEvent = new LogEventInfo
                {
                    Message = "Test Message",
                    Exception = outerException
                };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual("Test Message", jsonObject.Value<string>("short_message"));
                Assert.AreEqual("Test Message", jsonObject.Value<string>("full_message"));
                Assert.AreEqual(3, jsonObject.Value<int>("level"));
                Assert.AreEqual("TestFacility", jsonObject.Value<string>("facility"));
                Assert.AreEqual(null, jsonObject.Value<string>("_ExceptionSource"));
                const string expectedExceptionDetail =
                    "Outer Exception Detail - Inner Exception Detail - 1 - Inner Exception Detail - 2 - Inner Exception Detail - 3 - Inner Exception Detail - 4 - Inner Exception Detail - 5 - Inner Exception Detail - 6 - Inner Exception Detail - 7 - Inner Exception Detail - 8 - Inner Exception Detail - 9 - Inner Exception Detail - 10";
                Assert.AreEqual(expectedExceptionDetail, jsonObject.Value<string>("_ExceptionMessage"));
                Assert.AreEqual(null, jsonObject.Value<string>("_StackTrace"));
                Assert.AreEqual(null, jsonObject.Value<string>("_LoggerName"));
            }
            public void ShouldSetDefaultFacility(string facility)
            {
                var logEvent = new LogEventInfo {Message = "Test"};

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, facility);

                Assert.AreEqual("GELF", jsonObject.Value<string>("facility"));
            }
            public void ShouldHandleLongMessageCorrectly()
            {
                var logEvent = new LogEventInfo
                {
                    //The first 300 chars of lorem ipsum...
                    Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus interdum est in est cursus vitae pellentesque felis lobortis. Donec a orci quis ante viverra eleifend ac et quam. Donec imperdiet libero ut justo tincidunt non tristique mauris gravida. Fusce sapien eros, tincidunt a placerat nullam."
                };

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual(250, jsonObject.Value<string>("short_message").Length);
                Assert.AreEqual(300, jsonObject.Value<string>("full_message").Length);
            }
            public void ShouldCreateGelfJsonCorrectly()
            {
                var timestamp = DateTime.Now;
                var logEvent = new LogEventInfo
                                   {
                                       Message = "Test Log Message", 
                                       Level = LogLevel.Info, 
                                       TimeStamp = timestamp,
                                       LoggerName = "GelfConverterTestLogger"
                                   };
                logEvent.Properties.Add("customproperty1", "customvalue1");
                logEvent.Properties.Add("customproperty2", "customvalue2");
                logEvent.Properties.Add("custompropertyint", 199);
                logEvent.Properties.Add("custompropertyarray", new[]{1,2,3});

                var jsonObject = new GelfConverter().GetGelfJson(logEvent, "TestFacility");

                Assert.IsNotNull(jsonObject);
                Assert.AreEqual("1.0", jsonObject.Value<string>("version"));
                Assert.AreEqual(Dns.GetHostName(), jsonObject.Value<string>("host"));
                Assert.AreEqual("Test Log Message", jsonObject.Value<string>("short_message"));
                Assert.AreEqual("Test Log Message", jsonObject.Value<string>("full_message"));
                Assert.AreEqual(timestamp, jsonObject.Value<DateTime>("timestamp"));
                Assert.AreEqual(6, jsonObject.Value<int>("level"));
                Assert.AreEqual("TestFacility", jsonObject.Value<string>("facility"));
                Assert.AreEqual("", jsonObject.Value<string>("file"));
                Assert.AreEqual("", jsonObject.Value<string>("line"));

                Assert.AreEqual("customvalue1", jsonObject.Value<string>("_customproperty1"));
                Assert.AreEqual("customvalue2", jsonObject.Value<string>("_customproperty2"));
                Assert.AreEqual(199, jsonObject.Value<int>("_custompropertyint"));
                Assert.AreEqual(new[] { 1, 2, 3 }, jsonObject["_custompropertyarray"].ToObject<int[]>());
                Assert.AreEqual("GelfConverterTestLogger", jsonObject.Value<string>("_LoggerName"));
                
                //make sure that there are no other junk in there
                Assert.AreEqual(14, jsonObject.Count);
            }