Beispiel #1
0
        public void IsHandled_NullValue_InNewRelicDictionary()
        {
            // Arrange
            var testNRProperties = new Dictionary <string, string>()
            {
                { StringATestKey, StringTestValue },
                { NullTestKey, null }
            };

            var testEnricher = new TestEnricher()
                               .WithUserPropValue(IntegerTestKey, IntegerTestValue)
                               .WithNewRelicMetadataValue(testNRProperties);
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            testLogger.Warning(LogMessage);

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            Asserts.PropertyCountsMatch(testEnricher, countIntrinsicProperties, resultDic);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + IntegerTestKey, IntegerTestValue);
            Assert.That(resultDic, Does.Not.ContainKey(UserPropertyKeyPrefix + LinkingMetadataKey));
            Asserts.KeyAndValueMatch(resultDic, StringATestKey, StringTestValue);
            Asserts.KeyAndValueMatch(resultDic, NullTestKey, JsonValueKind.Null);
        }
Beispiel #2
0
        public void Output_ExceptionProperties_NoMessage()
        {
            // Arrange
            var testEnricher   = new TestEnricher();
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher);
            var testException  = new Exception(string.Empty);

            // Act
            try
            {
                TestHelpers.CreateStackTracedError(0, testException, 3);
            }
            catch (Exception ex)
            {
                testLogger.Error(ex, LogMessage);
            }

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            Asserts.PropertyCountsMatch(testEnricher, countIntrinsicProperties + 2, resultDic);
            Assert.That(resultDic, Does.Not.ContainKey("error.message"));
            Asserts.KeyAndValueMatch(resultDic, "error.class", testException.GetType().FullName);
            Asserts.KeyAndValueMatch(resultDic, "error.stack", testException.StackTrace);
        }
Beispiel #3
0
        public void Output_Escaping_AtSign()
        {
            // Arrange
            var testEnricher = new TestEnricher()
                               .WithUserPropValue(IntegerTestKey, IntegerTestValue)
                               .WithUserPropValue(SingleAtSignTestKey, BooleanTestValue)
                               .WithUserPropValue(DoubleAtSignTestKey, NullTestValue)
                               .WithNewRelicMetadataValue(new Dictionary <string, string>());
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            testLogger.Warning(LogMessage);

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            Asserts.PropertyCountsMatch(testEnricher, countIntrinsicProperties, resultDic);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + IntegerTestKey, IntegerTestValue);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + "@" + SingleAtSignTestKey, BooleanTestValue);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + DoubleAtSignTestKey, JsonValueKind.Null);
            Assert.That(resultDic, Does.Not.ContainKey(UserPropertyKeyPrefix + LinkingMetadataKey));
        }
Beispiel #4
0
        public void Output_Intrinsics_ThreadId()
        {
            const string ThreadIDKey       = "ThreadID";
            var          testThreadIDValue = _random.Next(0, int.MaxValue);

            // Arrange
            var testEnricher     = new TestEnricher();
            var threadIdEnricher = new TestEnricher()
                                   .WithUserPropValue(ThreadIDKey, testThreadIDValue);
            var formatter = new NewRelicFormatter()
                            .WithPropertyMapping(ThreadIDKey, NewRelicLoggingProperty.ThreadId);
            var testOutputSink = new TestSinkWithFormatter(formatter);
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher, threadIdEnricher);

            // Act
            testLogger.Warning(LogMessage);

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            //additional 1 for threadid
            Asserts.PropertyCountsMatch(testEnricher, countIntrinsicProperties + 1, resultDic);
            Assert.That(resultDic, Contains.Key("thread.id"));
            Assert.That(testOutputSink.InputsAndOutputs[0].LogEvent.Properties[ThreadIDKey].ToString(), Is.EqualTo(resultDic["thread.id"].GetString()));
        }
Beispiel #5
0
        public void Output_LinkingMetadataProperties()
        {
            // Arrange
            var testNRProperties = new Dictionary <string, string>()
            {
                { StringATestKey, "TestValue1" },
                { StringBTestKey, "TestValue2" }
            };

            var testEnricher = new TestEnricher()
                               .WithUserPropValue(IntegerTestKey, IntegerTestValue)
                               .WithNewRelicMetadataValue(testNRProperties);
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = SerilogTestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            testLogger.Warning(LogMessage);

            // Assert
            AssertNoSerilogErrorsAndCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = SerilogTestHelpers.DeserializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            AssertThatPropertyCountsMatch(testEnricher, CountIntrinsicProperties, resultDic);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + IntegerTestKey, IntegerTestValue);
            Assert.That(resultDic, Does.Not.ContainKey(UserPropertyKeyPrefix + LinkingMetadataKey));
            Asserts.KeyAndValueMatch(resultDic, StringATestKey, "TestValue1");
            Asserts.KeyAndValueMatch(resultDic, StringBTestKey, "TestValue2");
        }
Beispiel #6
0
        public void Output_ExceptionProperties()
        {
            // Arrange
            var testEnricher   = new TestEnricher();
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = SerilogTestHelpers.GetLogger(testOutputSink, testEnricher);
            var testException  = new InvalidOperationException(TestErrMsg);

            // Act
            try
            {
                TestHelpers.CreateStackTracedError(0, testException, 3);
            }
            catch (Exception ex)
            {
                testLogger.Error(ex, LogMessage);
            }

            // Assert
            AssertNoSerilogErrorsAndCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = SerilogTestHelpers.DeserializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            AssertThatPropertyCountsMatch(testEnricher, CountIntrinsicProperties + 3, resultDic);
            Asserts.KeyAndValueMatch(resultDic, "error.message", TestErrMsg);
            Asserts.KeyAndValueMatch(resultDic, "error.class", testException.GetType().FullName);
            Asserts.KeyAndValueMatch(resultDic, "error.stack", testException.StackTrace);
        }
Beispiel #7
0
        public void Mapping_NonReservedProperty_MapsProperly()
        {
            var testNRProperties = new Dictionary <string, string>()
            {
                { StringATestKey, "TestValue1" },
                { StringBTestKey, "TestValue2" }
            };

            var testEnricher = new TestEnricher()
                               .WithNewRelicMetadataValue(testNRProperties);

            // Build test data and configure formatter
            var expectedOutputs = new Dictionary <string, string>();
            var inputValues     = new Dictionary <string, int>();
            var testFormatter   = new NewRelicFormatter();

            foreach (var prop in _newRelicPropertiesNotReserved)
            {
                var propName  = Guid.NewGuid().ToString();
                var propValue = _random.Next(int.MaxValue);

                inputValues.Add(propName, propValue);
                expectedOutputs.Add(LoggingExtensions.GetOutputName(prop), propValue.ToString());

                testEnricher.WithUserPropValue(propName, propValue);
                testFormatter.WithPropertyMapping(propName, prop);
            }

            var testOutputSink = new TestSinkWithFormatter(testFormatter);

            var testLogger = TestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            testLogger.Warning(LogMessage);

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            foreach (var expectedOutput in expectedOutputs)
            {
                Asserts.KeyAndValueMatch(resultDic, expectedOutput.Key, expectedOutput.Value);
            }

            foreach (var inputVal in inputValues)
            {
                Assert.That(resultDic, Does.Not.ContainKey(inputVal.Key));
                Assert.That(resultDic, Does.Not.ContainKey(UserPropertyKeyPrefix + inputVal.Key));
            }
        }
Beispiel #8
0
        public void Output_Intrinsics_LogLevel(LogEventLevel level)
        {
            // Arrange
            var testEnricher   = new TestEnricher();
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            testLogger.Write(level, LogMessage);

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            Asserts.PropertyCountsMatch(testEnricher, countIntrinsicProperties, resultDic);
            Assert.That(resultDic, Contains.Key("log.level"));
            Assert.That(testOutputSink.InputsAndOutputs[0].LogEvent.Level.ToString(), Is.EqualTo(resultDic["log.level"].GetString()));
        }
Beispiel #9
0
        public void Output_Intrinsics_Timestamp_AsUnixTimestamp()
        {
            // Arrange
            var testEnricher   = new TestEnricher();
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            testLogger.Warning(LogMessage);

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            Asserts.PropertyCountsMatch(testEnricher, countIntrinsicProperties, resultDic);
            Assert.That(resultDic, Contains.Key("timestamp"));
            Assert.That(testOutputSink.InputsAndOutputs[0].LogEvent.Timestamp.ToUnixTimeMilliseconds(),
                        Is.EqualTo(resultDic["timestamp"].GetInt64()));
        }
Beispiel #10
0
        public void Output_Intrinsics_MessageAndTemplate()
        {
            // Arrange
            var testEnricher   = new TestEnricher();
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            const string template = "We have {value1}, {value2}, and {value3}.";

            testLogger.Warning(template, 1, "two", false);

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, template);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            Asserts.PropertyCountsMatch(testEnricher, countIntrinsicProperties + 3, resultDic);
            Asserts.KeyAndValueMatch(resultDic, "message", "We have 1, \"two\", and False.");
        }
Beispiel #11
0
        public void Output_ExceptionProperties_NoStackTrace()
        {
            // Arrange
            var testEnricher   = new TestEnricher();
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = SerilogTestHelpers.GetLogger(testOutputSink, testEnricher);
            var testException  = new Exception(TestErrMsg);

            // Act
            testLogger.Error(testException, LogMessage);

            // Assert
            AssertNoSerilogErrorsAndCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = SerilogTestHelpers.DeserializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            AssertThatPropertyCountsMatch(testEnricher, CountIntrinsicProperties + 2, resultDic);
            Assert.That(resultDic, Does.Not.ContainKey("error.stack"));
            Asserts.KeyAndValueMatch(resultDic, "error.message", TestErrMsg);
            Asserts.KeyAndValueMatch(resultDic, "error.class", testException.GetType().FullName);
        }
Beispiel #12
0
        public void Output_UserProperties()
        {
            // Arrange
            var testEnricher = new TestEnricher()
                               .WithUserPropValue(IntegerTestKey, IntegerTestValue)
                               .WithUserPropValue(BooleanTestKey, BooleanTestValue)
                               .WithUserPropValue(NullTestKey, NullTestValue)
                               .WithUserPropValue(StringATestKey, StringTestValue)
                               .WithUserPropValue(DictionaryTestKey, new Dictionary <string, object>()
            {
                { "DKeyA", "DValueA" }, { "DKeyB", 42 }
            })
                               .WithNewRelicMetadataValue(new Dictionary <string, string>());
            var testOutputSink = new TestSinkWithFormatter(new NewRelicFormatter());
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            testLogger.Warning(LogMessage);

            // Assert
            Asserts.NoSerilogErrorsCountOutputs(_testRunDebugLogs, testOutputSink.InputsAndOutputs, LogMessage);

            var resultDic = TestHelpers.SerializeOutputJSON(testOutputSink.InputsAndOutputs[0]);

            Asserts.PropertyCountsMatch(testEnricher, countIntrinsicProperties, resultDic);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + IntegerTestKey, IntegerTestValue);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + BooleanTestKey, BooleanTestValue);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + NullTestKey, JsonValueKind.Null);
            Asserts.KeyAndValueMatch(resultDic, UserPropertyKeyPrefix + StringATestKey, StringTestValue);
            Assert.That(resultDic, Contains.Key(UserPropertyKeyPrefix + DictionaryTestKey));
            Assert.That(resultDic[UserPropertyKeyPrefix + DictionaryTestKey].ValueKind, Is.EqualTo(JsonValueKind.Object));
            var innerDic = JsonSerializer.Deserialize <Dictionary <string, JsonElement> >(resultDic[UserPropertyKeyPrefix + DictionaryTestKey].ToString());

            Assert.That(innerDic, Contains.Key("DKeyA"));
            Assert.That(innerDic["DKeyA"].GetString(), Is.EqualTo("DValueA"));
            Assert.That(innerDic, Contains.Key("DKeyB"));
            Assert.That(innerDic["DKeyB"].GetUInt32(), Is.EqualTo(42));
            Assert.That(resultDic, Does.Not.ContainKey(UserPropertyKeyPrefix + LinkingMetadataKey));
        }
Beispiel #13
0
        public void IsHandled_Exception()
        {
            // Arrange
            // Setup formatter to fail part-way through emitting the JSON string.
            // Want to verify that a partial JSON string is not written out.
            var testEnricher = new TestEnricher()
                               .WithUserPropValue("StartTestKey", "This is the start")
                               .WithUserPropValue("ErrorTestKey", TestErrMsg)
                               .WithUserPropValue("EndTestKey", "This is the end");

            var testFormatter  = new TestFormatterThatThrowException();
            var testOutputSink = new TestSinkWithFormatter(testFormatter);
            var testLogger     = TestHelpers.GetLogger(testOutputSink, testEnricher);

            // Act
            testLogger.Warning(LogMessage);

            // Assert
            Assert.That(_testRunDebugLogs.Count, Is.EqualTo(1));
            Assert.That(testOutputSink.InputsAndOutputs.Count, Is.EqualTo(1));
            Assert.That(testOutputSink.InputsAndOutputs[0].LogEvent.MessageTemplate.Text, Is.EqualTo(LogMessage));
            Assert.That(testOutputSink.InputsAndOutputs[0].FormattedOutput, Is.Null);
        }
Beispiel #14
0
        private static void AssertThatPropertyCountsMatch(TestEnricher enricher, int countIntrinsics, Dictionary <string, JsonElement> jsonAsDic)
        {
            var expectedCount = enricher.CountUserProps + enricher.CountNewRelicProps + countIntrinsics;

            Assert.That(jsonAsDic.Count, Is.EqualTo(expectedCount), "Output Json Property Count Mismatch");
        }