diff --git a/src/Agent/NewRelic/Agent/Core/Agent.cs b/src/Agent/NewRelic/Agent/Core/Agent.cs index e9e5f7291a..35780e3bf8 100644 --- a/src/Agent/NewRelic/Agent/Core/Agent.cs +++ b/src/Agent/NewRelic/Agent/Core/Agent.cs @@ -439,14 +439,15 @@ public void RecordLogMessage(string frameworkName, object logEvent, Func _configurationService.Configuration.LogEventCollectorEnabled) + .Returns(true); + Mock.Arrange(() => _configurationService.Configuration.ContextDataEnabled) + .Returns(true); + + var timestamp = DateTime.Now; + var timestampUnix = timestamp.ToUnixTimeMilliseconds(); + var level = "DEBUG"; + string message = null; + var contextData = new Dictionary() { { "key1", "value1" }, { "key2", 1 } }; + + Func getLevelFunc = (l) => level; + Func getTimestampFunc = (l) => timestamp; + Func getMessageFunc = (l) => message; + Func getLogExceptionFunc = (l) => null; + Func> getContextDataFunc = (l) => contextData; + + var spanId = "spanid"; + var traceId = "traceid"; + var loggingFramework = "testFramework"; + + var xapi = _agent as IAgentExperimental; + xapi.RecordLogMessage(loggingFramework, new object(), getTimestampFunc, getLevelFunc, getMessageFunc, getLogExceptionFunc, getContextDataFunc, spanId, traceId); + + // Access the private collection of events to get the number of add attempts. + var privateAccessor = new PrivateAccessor(_logEventAggregator); + var logEvents = privateAccessor.GetField("_logEvents") as ConcurrentPriorityQueue>; + + var logEvent = logEvents?.FirstOrDefault()?.Data; + Assert.AreEqual(1, logEvents.Count); + Assert.IsNotNull(logEvent); + Assert.AreEqual(timestampUnix, logEvent.TimeStamp); + Assert.AreEqual(level, logEvent.Level); + Assert.AreEqual(message, logEvent.Message); + Assert.AreEqual(spanId, logEvent.SpanId); + Assert.AreEqual(traceId, logEvent.TraceId); + Assert.AreEqual(contextData, logEvent.ContextData); + Assert.IsNotNull(logEvent.Priority); + } + + [Test] + public void RecordLogMessage_NoTransaction_NoMessage_NoException_NoContextData_DropsEvent() { Mock.Arrange(() => _configurationService.Configuration.LogEventCollectorEnabled) .Returns(true); @@ -1473,7 +1516,51 @@ public void RecordLogMessage_WithTransaction_NoMessage_WithException_Success() } [Test] - public void RecordLogMessage_WithTransaction_NoMessage_NoException_DropsEvent() + public void RecordLogMessage_WithTransaction_NoMessage_NoException_WithContextData_Success() + { + Mock.Arrange(() => _configurationService.Configuration.LogEventCollectorEnabled) + .Returns(true); + Mock.Arrange(() => _configurationService.Configuration.ContextDataEnabled) + .Returns(true); + + var timestamp = DateTime.Now; + var timestampUnix = timestamp.ToUnixTimeMilliseconds(); + var level = "DEBUG"; + string message = null; + var contextData = new Dictionary() { { "key1", "value1" }, { "key2", 1 } }; + + Func getLevelFunc = (l) => level; + Func getTimestampFunc = (l) => timestamp; + Func getMessageFunc = (l) => message; + Func getLogExceptionFunc = (l) => null; + Func> getContextDataFunc = (l) => contextData; + + var spanId = "spanid"; + var traceId = "traceid"; + var loggingFramework = "testFramework"; + + SetupTransaction(); + var transaction = _transactionService.GetCurrentInternalTransaction(); + var priority = transaction.Priority; + + var xapi = _agent as IAgentExperimental; + xapi.RecordLogMessage(loggingFramework, new object(), getTimestampFunc, getLevelFunc, getMessageFunc, getLogExceptionFunc, getContextDataFunc, spanId, traceId); + + var harvestedLogEvents = transaction.HarvestLogEvents(); + var logEvent = harvestedLogEvents.FirstOrDefault(); + Assert.AreEqual(1, harvestedLogEvents.Count); + Assert.IsNotNull(logEvent); + Assert.AreEqual(timestampUnix, logEvent.TimeStamp); + Assert.AreEqual(level, logEvent.Level); + Assert.AreEqual(message, logEvent.Message); + Assert.AreEqual(spanId, logEvent.SpanId); + Assert.AreEqual(traceId, logEvent.TraceId); + Assert.AreEqual(contextData, logEvent.ContextData); + Assert.AreEqual(priority, logEvent.Priority); + } + + [Test] + public void RecordLogMessage_WithTransaction_NoMessage_NoException_NoContextData_DropsEvent() { Mock.Arrange(() => _configurationService.Configuration.LogEventCollectorEnabled) .Returns(true);