Exemple #1
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()));
        }
Exemple #2
0
        public void Mapping_ReservedProperty_ThrowsException()
        {
            // Arrange
            var testFormatter = new NewRelicFormatter();

            // Act and Assert
            foreach (var prop in _newRelicPropertiesReserved)
            {
                Assert.That(() => testFormatter.WithPropertyMapping(Guid.NewGuid().ToString(), prop), Throws.Exception.TypeOf <InvalidOperationException>());
            }
        }
Exemple #3
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));
            }
        }