예제 #1
0
        public NewRelicFormatter WithPropertyMapping(string propertyName, NewRelicLoggingProperty outputAsNewRelicProperty)
        {
            if (_reservedProperties.Contains(outputAsNewRelicProperty))
            {
                throw new InvalidOperationException($"The New Relic Serilog Extension does not allow mapping of property {outputAsNewRelicProperty}");
            }

            _propertyMappings[propertyName] = LoggingExtensions.GetOutputName(outputAsNewRelicProperty);
            return(this);
        }
        private void SetInstrinsics(Dictionary <string, object> dictionary, LoggingEvent loggingEvent)
        {
            if (dictionary == null)
            {
                return;
            }

            dictionary.Add(LoggingExtensions.GetOutputName(NewRelicLoggingProperty.Timestamp), loggingEvent.TimeStamp.ToUnixTimeMilliseconds());
            dictionary.Add(LoggingExtensions.GetOutputName(NewRelicLoggingProperty.ThreadName), loggingEvent.ThreadName);
            dictionary.Add(LoggingExtensions.GetOutputName(NewRelicLoggingProperty.MessageText), loggingEvent.RenderedMessage);
            dictionary.Add(LoggingExtensions.GetOutputName(NewRelicLoggingProperty.LogLevel), loggingEvent.Level);
        }
예제 #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));
            }
        }
        private void SetExceptionData(Dictionary <string, object> dictionary, LoggingEvent loggingEvent)
        {
            if (dictionary == null)
            {
                return;
            }

            if (loggingEvent.ExceptionObject != null)
            {
                if (!string.IsNullOrEmpty(loggingEvent.ExceptionObject.Message))
                {
                    dictionary.Add(LoggingExtensions.GetOutputName(NewRelicLoggingProperty.ErrorMessage), loggingEvent.ExceptionObject.Message);
                }

                if (!string.IsNullOrEmpty(loggingEvent.ExceptionObject.StackTrace))
                {
                    dictionary.Add(LoggingExtensions.GetOutputName(NewRelicLoggingProperty.ErrorStack), loggingEvent.ExceptionObject.StackTrace);
                }

                dictionary.Add(LoggingExtensions.GetOutputName(NewRelicLoggingProperty.ErrorClass), loggingEvent.ExceptionObject.GetType().ToString());
            }
        }