public void Write(ILogEventInfoProxy?logEventInfo) { if (logEventInfo is null) { return; } if (logEventInfo.Level.Ordinal < _minimumLevel) { return; } // Nlog automatically includes all the properties from the event, so don't need fallback properties var mappedProperties = _baseProxy?.GetAllProperties(logEventInfo); // We render the event to a string immediately as we need to capture the properties // This is more expensive from a CPU perspective, but is necessary as the properties // won't necessarily be serialized correctly otherwise (e.g. dd_span_id/dd_trace_id) var logEvent = new LogEntry(logEventInfo, mappedProperties, fallbackProperties: null); var logFormatter = _formatter ?? TracerManager.Instance.DirectLogSubmission.Formatter; var serializedLog = NLogLogFormatter.FormatLogEvent(logFormatter, logEvent); _sink.EnqueueLog(new NLogDatadogLogEvent(serializedLog)); }
public void Write(ILogEventInfoLegacyProxy?logEventInfo) { if (logEventInfo is null) { return; } if (logEventInfo.Level.Ordinal < _minimumLevel) { return; } var contextProperties = _getProperties?.Invoke(); var eventProperties = logEventInfo.Properties is { Count : > 0 } props?props : null; // We render the event to a string immediately as we need to capture the properties // This is more expensive from a CPU perspective, but is necessary as the properties // won't necessarily be serialized correctly otherwise (e.g. dd_span_id/dd_trace_id) var logEvent = new LogEntry(logEventInfo, contextProperties, eventProperties); var logFormatter = _formatter ?? TracerManager.Instance.DirectLogSubmission.Formatter; var serializedLog = NLogLogFormatter.FormatLogEvent(logFormatter, logEvent); _sink.EnqueueLog(new NLogDatadogLogEvent(serializedLog)); }
public void SerializesEventCorrectlyWhenUsingFallbackProperties() { var logEvent = GetLogEvent(); var log = NLogHelper.GetLogEventProxy(logEvent); var fallback = new Dictionary <object, object> { { "Value", 123 }, { "OtherProperty", 62 }, }; var wrapper = new LogEntry(log, properties: null, fallback); var formatter = LogSettingsHelper.GetFormatter(); var sb = new StringBuilder(); NLogLogFormatter.FormatLogEvent(formatter, sb, wrapper); var actual = sb.ToString(); var expected = @"{""@t"":""2021-09-13T10:40:57.0000000Z"",""@m"":""This is a test with a 123"",""@l"":""Debug"",""@x"":""System.InvalidOperationException: Oops, just a test!"",""Value"":123,""OtherProperty"":62,""@i"":""e5450052"",""ddsource"":""csharp"",""service"":""MyTestService"",""dd_env"":""integration_tests"",""dd_version"":""1.0.0"",""host"":""some_host""}"; actual.Should().Be(expected); }