Esempio n. 1
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");

                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("GelfConverterTestLogger", jsonObject.Value <string>("_LoggerName"));

                //make sure that there are no other junk in there
                Assert.AreEqual(12, jsonObject.Count);
            }
Esempio n. 2
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);
        }
        public void CanRenderGelf()
        {
            var loggerName = "TestLogger";
            var facility   = "TestFacility";
            var dateTime   = DateTime.Now;
            var message    = "hello, gelf :)";
            var logLevel   = LogLevel.Info;
            var hostname   = Dns.GetHostName();

            var gelfRenderer = new GelfLayoutRenderer();

            gelfRenderer.Facility = facility;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
            };

            var renderedGelf     = gelfRenderer.Render(logEvent);
            var expectedDateTime = GelfConverter.ToUnixTimeStamp(dateTime);
            var expectedGelf     = string.Format("{{\"facility\":\"{0}\",\"file\":\"\",\"full_message\":\"{1}\",\"host\":\"{2}\",\"level\":{3},\"line\":0,\"short_message\":\"{4}\",\"timestamp\":{5},\"version\":\"1.1\",\"_LoggerName\":\"{6}\"}}", facility, message, hostname, logLevel.GetOrdinal(), message, expectedDateTime, loggerName);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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"));
            }
        public void CanRenderGelfException()
        {
            var loggerName = "TestLogger";
            var facility   = "TestFacility";
            var dateTime   = DateTime.Now;
            var message    = "hello, gelf :)";
            var logLevel   = LogLevel.Fatal;
            var hostname   = Dns.GetHostName();
            var exception  = FakeException.Throw();

            var gelfRenderer = new GelfLayoutRenderer();

            gelfRenderer.Facility = facility;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
                Exception  = exception,
            };

            var          renderedGelf      = gelfRenderer.Render(logEvent);
            var          expectedDateTime  = GelfConverter.ToUnixTimeStamp(dateTime);
            const string exceptionPath     = "c:\\\\GitHub\\\\NLog.GelfLayout\\\\src\\\\NLog.Layouts.GelfLayout.Test\\\\FakeException.cs";
            const string expectedException = "\"_ExceptionSource\":\"NLog.Layouts.GelfLayout.Test\",\"_ExceptionMessage\":\"funny exception :D\",\"_StackTrace\":\"System.Exception: funny exception :D ---> System.Exception: very funny exception ::D\\r\\n   --- End of inner exception stack trace ---\\r\\n   at NLog.Layouts.GelfLayout.Test.FakeException.Throw() in " + exceptionPath + ":line 9\"";
            var          expectedGelf      = string.Format("{{\"facility\":\"{0}\",\"file\":\"\",\"full_message\":\"{1}\",\"host\":\"{2}\",\"level\":{3},\"line\":0,\"short_message\":\"{4}\",\"timestamp\":{5},\"version\":\"1.1\",{6},\"_LoggerName\":\"{7}\"}}", facility, message, hostname, logLevel.GetOrdinal(), message, expectedDateTime, expectedException, loggerName);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }
Esempio n. 7
0
        public void CanRenderGelfException()
        {
            var loggerName   = "TestLogger";
            var facility     = "TestFacility";
            var dateTime     = DateTime.Now;
            var message      = "hello, gelf :)";
            var logLevel     = LogLevel.Fatal;
            var hostname     = Dns.GetHostName();
            var exception    = FakeException.Throw();
            var gelfRenderer = new GelfLayoutRenderer();

            gelfRenderer.Facility = facility;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
                Exception  = exception,
            };
            var    renderedGelf       = gelfRenderer.Render(logEvent);
            var    expectedDateTime   = GelfConverter.ToUnixTimeStamp(dateTime);
            string executingDirectory = Directory.GetCurrentDirectory();
            string srcDirectory       =
                executingDirectory.Substring(0, executingDirectory.IndexOf("bin")).Replace("\\", "\\\\");
            string exceptionPath     = $"{srcDirectory}FakeException.cs";
            string expectedException =
                "\"_ExceptionSource\":\"NLog.Layouts.GelfLayout.Test\","
                + "\"_ExceptionMessage\":\"funny exception :D\","
                + "\"_ExceptionType\":\"System.ArgumentException\","
                + "\"_StackTrace\":\"System.ArgumentException: funny exception :D\\r\\n ---> System.Exception: very funny "
                + "exception ::D\\r\\n   --- End of inner exception stack trace ---\\r\\n   "
                + "at NLog.Layouts.GelfLayout.Test.FakeException.Throw() in "
                + exceptionPath
                + ":line 9\"";
            var expectedGelf = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                             "{{\"facility\":\"{0}\","
                                             + "\"file\":\"TestLogger\","
                                             + "\"full_message\":\"{1}\","
                                             + "\"host\":\"{2}\","
                                             + "\"level\":{3},"
                                             + "\"line\":0,"
                                             + "\"short_message\":\"{4}\","
                                             + "\"timestamp\":{5},"
                                             + "\"version\":\"1.1\","
                                             + "{6},"
                                             + "\"_LoggerName\":\"{7}\"}}",
                                             facility,
                                             message,
                                             hostname,
                                             logLevel.GetOrdinal(),
                                             message,
                                             expectedDateTime,
                                             expectedException,
                                             loggerName);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }
Esempio n. 8
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"));
            }
        public void ShouldGetGelfJsonAddMappedDiagnosticsLogicalContextData()
        {
            MappedDiagnosticsLogicalContext.Set("test", "value");

            var logEvent = LogEventInfo.Create(LogLevel.Info, "loggerName", null, "message");

            var converter = new GelfConverter();

            // Act
            var gelfJson = converter.GetGelfJson(logEvent, "facility");

            Assert.Equal("value", gelfJson.Value <string>("_test"));
        }
Esempio n. 10
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_"));
            }
Esempio n. 11
0
        public void CanRenderGelfIncludeMdlc()
        {
            var loggerName   = "TestLogger";
            var facility     = "TestFacility";
            var dateTime     = DateTime.Now;
            var message      = "hello, gelf :)";
            var logLevel     = LogLevel.Info;
            var hostname     = Dns.GetHostName();
            var gelfRenderer = new GelfLayoutRenderer();

            gelfRenderer.Facility    = facility;
            gelfRenderer.IncludeMdlc = true;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
            };

            Guid requestId = Guid.NewGuid();

            using (var mdlc = NLog.MappedDiagnosticsLogicalContext.SetScoped("RequestId", requestId))
            {
                var renderedGelf     = gelfRenderer.Render(logEvent);
                var expectedDateTime = GelfConverter.ToUnixTimeStamp(dateTime);
                var expectedGelf     = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                     "{{\"facility\":\"{0}\","
                                                     + "\"file\":\"TestLogger\","
                                                     + "\"full_message\":\"{1}\","
                                                     + "\"host\":\"{2}\","
                                                     + "\"level\":{3},"
                                                     + "\"line\":0,"
                                                     + "\"short_message\":\"{4}\","
                                                     + "\"timestamp\":{5},"
                                                     + "\"version\":\"1.1\","
                                                     + "\"_LoggerName\":\"{6}\","
                                                     + "\"_RequestId\":\"{7}\"}}",
                                                     facility,
                                                     message,
                                                     hostname,
                                                     logLevel.GetOrdinal(),
                                                     message,
                                                     expectedDateTime,
                                                     loggerName,
                                                     requestId);
                Assert.AreEqual(expectedGelf, renderedGelf);
            }
        }
Esempio n. 12
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);
            }
Esempio n. 13
0
        public void CanRenderGelfAdditionalFields()
        {
            var loggerName = "TestLogger";
            var facility   = "TestFacility";
            var dateTime   = DateTime.Now;
            var message    = "hello, gelf :)";
            var logLevel   = LogLevel.Info;
            var hostname   = Dns.GetHostName();
            var gelfLayout = new GelfLayout();

            gelfLayout.Facility = facility;
            gelfLayout.ExtraFields.Add(new GelfField("ThreadId", "${threadid}")
            {
                PropertyType = typeof(int)
            });

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
            };

            int threadId         = System.Threading.Thread.CurrentThread.ManagedThreadId;
            var renderedGelf     = gelfLayout.Render(logEvent);
            var expectedDateTime = GelfConverter.ToUnixTimeStamp(dateTime);
            var expectedGelf     = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                 "{{\"facility\":\"{0}\","
                                                 + "\"file\":\"TestLogger\","
                                                 + "\"full_message\":\"{1}\","
                                                 + "\"host\":\"{2}\","
                                                 + "\"level\":{3},"
                                                 + "\"line\":0,"
                                                 + "\"short_message\":\"{4}\","
                                                 + "\"timestamp\":{5},"
                                                 + "\"version\":\"1.1\","
                                                 + "\"_LoggerName\":\"{6}\","
                                                 + "\"_ThreadId\":{7}}}",
                                                 facility,
                                                 message,
                                                 hostname,
                                                 logLevel.GetOrdinal(),
                                                 message,
                                                 expectedDateTime,
                                                 loggerName,
                                                 threadId);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }
Esempio n. 14
0
        public void ShouldNotChangeLogEvent()
        {
            // arrange
            var logEvent = new LogEventInfo
            {
                Message = "Message",
            };

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

            // assert
            jsonObject1.Should().BeEquivalentTo(jsonObject2);
        }
Esempio n. 15
0
        public void ShouldNotChangeLogEvent()
        {
            // arrange
            var logEvent = new LogEventInfo
            {
                Message = "Message",
            };

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

            // assert
            jsonObject1.Should().BeEquivalentTo(jsonObject2);
        }
Esempio n. 16
0
        public void CanRenderGelfWithBadObject()
        {
            var loggerName = "TestLogger";
            var facility   = "TestFacility";
            var dateTime   = DateTime.Now;
            var message    = "hello, gelf :)";
            var logLevel   = LogLevel.Info;
            var hostname   = Dns.GetHostName();
            var gelfLayout = new GelfLayout();

            gelfLayout.Facility             = facility;
            gelfLayout.IncludeAllProperties = true;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
            };

            logEvent.Properties["BadBoy"] = new BadLogObject();

            var renderedGelf     = gelfLayout.Render(logEvent);
            var expectedDateTime = GelfConverter.ToUnixTimeStamp(dateTime);
            var expectedGelf     = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                 "{{\"facility\":\"{0}\","
                                                 + "\"file\":\"TestLogger\","
                                                 + "\"full_message\":\"{1}\","
                                                 + "\"host\":\"{2}\","
                                                 + "\"level\":{3},"
                                                 + "\"line\":0,"
                                                 + "\"short_message\":\"{4}\","
                                                 + "\"timestamp\":{5},"
                                                 + "\"version\":\"1.1\","
                                                 + "\"_BadBoy\":{{\"BadArray\":[],\"BadProperty\":\"{6}\"}},"
                                                 + "\"_LoggerName\":\"{7}\"}}",
                                                 facility,
                                                 message,
                                                 hostname,
                                                 logLevel.GetOrdinal(),
                                                 message,
                                                 expectedDateTime,
                                                 typeof(BadLogObject).Assembly.ToString(),
                                                 loggerName);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }
Esempio n. 17
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);
        }
Esempio n. 18
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");
        }
            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"));
            }
            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"));
            }
Esempio n. 21
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 WhenLogErrorEvent_ThenErrorMessageBuilderShouldBeCalled()
        {
            var errorBuilder   = new Mock <IMessageBuilder>();
            var messageBuilder = new Mock <IMessageBuilder>();

            var messageBuilders = new Dictionary <BuilderType, Lazy <IMessageBuilder> >
            {
                [BuilderType.Exception] = new Lazy <IMessageBuilder>(() => errorBuilder.Object),
                [BuilderType.Message]   = new Lazy <IMessageBuilder>(() => messageBuilder.Object)
            };

            GelfConverter target = new GelfConverter(messageBuilders);

            var simpleEvent = LogEventSource.GetErrorEvent(DateTimeOffset.Now);

            target.GetGelfJson(simpleEvent);

            errorBuilder.Verify(c => c.Build(simpleEvent), Times.Once);
            messageBuilder.Verify(c => c.Build(simpleEvent), Times.Never);
        }
Esempio n. 23
0
            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"));
            }
Esempio n. 24
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"));
            }
Esempio n. 25
0
            public void ShouldCreateGelfJsonCorrectly()
            {
                var timestamp = DateTime.UtcNow;

                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");
                var unixTimestamp = (int)timestamp.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

                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(unixTimestamp, jsonObject.Value <int>("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);
            }
Esempio n. 26
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"));
        }
Esempio n. 27
0
        public void ShouldSerializeObjectsWhenEnabled()
        {
            // arrange
            var obj = new TestObject {
                Text = "Hello world!"
            };

            obj.InnerObject = obj;
            var logEvent = new LogEventInfo
            {
                Message    = obj.Text,
                Properties =
                {
                    { "test", obj },
                },
            };

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

            // assert
            jsonObject["_test"] !["Text"] !.Value <string>().Should().Be(obj.Text);
Esempio n. 28
0
        public void CanRenderGelf11()
        {
            var loggerName   = "TestLogger";
            var dateTime     = DateTime.Now;
            var message      = "hello, gelf :)";
            var logLevel     = LogLevel.Info;
            var hostname     = Dns.GetHostName();
            var gelfRenderer = new GelfLayoutRenderer();

            gelfRenderer.IncludeLegacyFields = false;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
            };

            var renderedGelf     = gelfRenderer.Render(logEvent);
            var expectedDateTime = GelfConverter.ToUnixTimeStamp(dateTime);
            var expectedGelf     = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                 "{{\"version\":\"1.1\","
                                                 + "\"host\":\"{0}\","
                                                 + "\"short_message\":\"{1}\","
                                                 + "\"full_message\":\"{2}\","
                                                 + "\"timestamp\":{3},"
                                                 + "\"level\":{4},"
                                                 + "\"_LoggerName\":\"{5}\"}}",
                                                 hostname,
                                                 message,
                                                 message,
                                                 expectedDateTime,
                                                 logLevel.GetOrdinal(),
                                                 loggerName);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }
Esempio n. 29
0
        public void ShouldHandleMessageWithSelfReferenceLoopCorrectly()
        {
            // arrange
            var obj = new TestObject {
                Text = "Hello world!"
            };

            obj.InnerObject = obj;
            var logEvent = new LogEventInfo
            {
                Message    = obj.Text,
                Properties =
                {
                    { "test", obj },
                },
            };

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

            // assert
            jsonObject.Should().NotBeNull();
        }
            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);
            }
            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 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_"));
            }
            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 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"));
            }
Esempio n. 35
0
        public void CanRenderGelfWithNonStringJsonValues()
        {
            var         loggerName   = "TestLogger";
            var         facility     = "TestFacility";
            var         dateTime     = DateTime.Now;
            var         message      = $"stringVal 1 {TestMsgEnum.Enum1} {dateTime.AddMinutes(-1)}";
            var         logLevel     = LogLevel.Info;
            var         hostname     = Dns.GetHostName();
            var         gelfRenderer = new GelfLayoutRenderer();
            string      stringKey    = "stringKey";
            string      stringVal    = "stringVal";
            string      intKey       = "intKey";
            int         intVal       = 1;
            string      enumKey      = "enumKey";
            TestMsgEnum enumVal      = TestMsgEnum.Enum1;
            string      dateTimeKey  = "dateTimeKey";
            DateTime    dateTimeVal  = dateTime.AddMinutes(-1);

            gelfRenderer.Facility = facility;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
                Properties =
                {
                    { stringKey,   stringVal   },
                    { intKey,      intVal      },
                    { enumKey,     enumVal     },
                    { dateTimeKey, dateTimeVal }
                }
            };

            var renderedGelf       = gelfRenderer.Render(logEvent);
            var expectedDateTime   = GelfConverter.ToUnixTimeStamp(dateTime);
            var expectedProperties = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                   "\"_{0}\":\"{1}\",\"_{2}\":{3},\"_{4}\":\"{5}\",\"_{6}\":{7}",
                                                   stringKey,
                                                   stringVal,
                                                   intKey,
                                                   intVal,
                                                   enumKey,
                                                   enumVal,
                                                   dateTimeKey,
                                                   GelfConverter.ToUnixTimeStamp(dateTimeVal));
            var expectedGelf = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                             "{{\"facility\":\"{0}\","
                                             + "\"file\":\"TestLogger\","
                                             + "\"full_message\":\"{1}\","
                                             + "\"host\":\"{2}\","
                                             + "\"level\":{3},"
                                             + "\"line\":0,"
                                             + "\"short_message\":\"{4}\","
                                             + "\"timestamp\":{5},"
                                             + "\"version\":\"1.1\","
                                             + "{6},"
                                             + "\"_LoggerName\":\"{7}\"}}",
                                             facility,
                                             message,
                                             hostname,
                                             logLevel.GetOrdinal(),
                                             message,
                                             expectedDateTime,
                                             expectedProperties,
                                             loggerName);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }