public void CanReverseDuckTypeTarget() { var targetType = typeof(Target); var target = NLogHelper.CreateTarget(new NullDatadogSink(), DirectSubmissionLogLevel.Debug); var proxy = NLogCommon <Target> .CreateNLogTargetProxy(target); proxy.Should().NotBeNull(); proxy.GetType().Should().BeDerivedFrom(targetType); var message = "the message"; var logInfo = new AsyncLogEventInfo(LogEventInfo.Create(LogLevel.Error, "test", message), _ => { }); var typedProxy = ((Target)proxy); typedProxy.WriteAsyncLogEvent(logInfo); // should not throw #if NLOG_45 var proxyOfProxy = proxy.DuckCast <ITargetWithContextBaseProxy>(); proxyOfProxy.Should().NotBeNull(); var results = proxyOfProxy.GetAllProperties(logInfo.LogEvent.DuckCast <ILogEventInfoProxy>()); results.Should().NotBeNull(); target.SetBaseProxy(proxyOfProxy); #endif }
public void ShouldNotEmitLogWhenNotEnabled() { var sink = new NLogHelper.TestSink(); var nlogSink = NLogHelper.CreateTarget(sink, DirectSubmissionLogLevel.Warning); var targetProxy = (global::NLog.Targets.Target) NLogCommon <LoggingConfiguration> .CreateNLogTargetProxy(nlogSink); var level = LogLevel.Info; var logEvent = new LogEventInfo(level, nameof(LoggerEnqueuesLogMessage), "This is some value"); var proxy = NLogHelper.GetLogEventProxy(logEvent); nlogSink.Write(proxy); sink.Events.Should().BeEmpty(); }
public void LoggerEnqueuesLogMessage() { var sink = new NLogHelper.TestSink(); var nlogSink = NLogHelper.CreateTarget(sink, DirectSubmissionLogLevel.Debug); var targetProxy = (global::NLog.Targets.Target) NLogCommon <LoggingConfiguration> .CreateNLogTargetProxy(nlogSink); var level = LogLevel.Error; var logEvent = new LogEventInfo(level, nameof(LoggerEnqueuesLogMessage), "This is some value"); var proxy = NLogHelper.GetLogEventProxy(logEvent); nlogSink.Write(proxy); sink.Events.Should().ContainSingle(); }
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); }
public void LoggerIncludesPropertiesInLog() { var formatter = LogSettingsHelper.GetFormatter(); var sink = new NLogHelper.TestSink(); var target = NLogHelper.CreateTarget(sink, DirectSubmissionLogLevel.Debug); var targetProxy = NLogCommon <LoggingConfiguration> .CreateNLogTargetProxy(target); var config = new LoggingConfiguration(); NLogHelper.AddTargetToConfig(config, targetProxy); var logFactory = new LogFactory(config); var logger = logFactory.GetLogger(nameof(LoggerIncludesPropertiesInLog)); // We don't currently record NDC/NDLC #if NLOG_45 var messageTemplate = "This is a message with {Value}"; #else var messageTemplate = "This is a message with {0}"; #endif var mdcKey = "some mdcKey"; var mdcValue = "some mdcValue"; #if !NLOG_2 var mdclKey = "some mdclKey"; var mdclValue = "some mdclValue"; #endif // var nestedScope = "some nested name"; // var nestedDictionary = new Dictionary<string, object> { { "nlcKey", 657 } }; // var dictValues = nestedDictionary.First(); try { MappedDiagnosticsContext.Set(mdcKey, mdcValue); #if !NLOG_2 MappedDiagnosticsLogicalContext.Set(mdclKey, mdclValue); #endif logger.Error(messageTemplate, 123); } finally { MappedDiagnosticsContext.Remove(mdcKey); #if !NLOG_2 MappedDiagnosticsLogicalContext.Remove(mdclKey); #endif } var logEvent = sink.Events.Should().ContainSingle().Subject; // get the rendered log var sb = new StringBuilder(); logEvent.Format(sb, formatter); var log = sb.ToString(); log.Should() .Contain("This is a message with 123") .And.Contain(mdcKey) .And.Contain(mdcValue) #if !NLOG_2 .And.Contain(mdclKey) .And.Contain(mdclValue) #endif // .And.Contain(nestedScope) // .And.Contain(dictValues.Key) // .And.Contain(dictValues.Value.ToString()) .And.Contain(DirectSubmissionLogLevelExtensions.Error); }