예제 #1
0
        public void CanDuckTypeLogInfoInLegacyNLog()
        {
#if NLOG_2
            var instance = new LogEventInfo(
                level: LogLevel.Fatal,
                loggerName: "LoggerName",
                message: "Some message {0}",
                exception: new InvalidOperationException(),
                formatProvider: null,
                parameters: new object[] { 123 });
#else
            var instance = LogEventInfo.Create(
                logLevel: LogLevel.Fatal,
                loggerName: "LoggerName",
                message: "Some message {0}",
                exception: new InvalidOperationException(),
                formatProvider: null,
                parameters: new object[] { 123 });
#endif
            var duck = instance.DuckCast <LogEventInfoLegacyProxy>();
            duck.Should().NotBeNull();
            duck.Level.Should().NotBeNull();
            duck.Level.Ordinal.Should().Be(LogLevel.Fatal.Ordinal);
            duck.LoggerName.Should().Be(instance.LoggerName);
            duck.FormattedMessage.Should().Be(instance.FormattedMessage);
            duck.Exception.Should().Be(instance.Exception);
            duck.HasProperties.Should().BeFalse();

            instance.Properties["TestKey"] = "TestValue";
            duck.HasProperties.Should().BeTrue();
            duck.Properties.Should().ContainKey("TestKey").WhichValue.Should().Be("TestValue");
        }
예제 #2
0
        public void CanDuckTypeLogInfoInModernNLog()
        {
            var instance = LogEventInfo.Create(
                logLevel: LogLevel.Fatal,
                loggerName: "LoggerName",
                message: "Some message {Value}",
                exception: new InvalidOperationException(),
                formatProvider: null,
                parameters: new object[] { 123 });

            var duck = instance.DuckCast <ILogEventInfoProxy>();

            duck.Should().NotBeNull();
            duck.Level.Should().NotBeNull();
            duck.Level.Ordinal.Should().Be(LogLevel.Fatal.Ordinal);
            duck.FormattedMessage.Should().Be(instance.FormattedMessage);
            duck.Exception.Should().Be(instance.Exception);
            duck.HasProperties.Should().BeTrue();

            var instanceWithoutProperties = LogEventInfo.Create(
                logLevel: LogLevel.Fatal,
                loggerName: "LoggerName",
                message: "Some message");

            instanceWithoutProperties.HasProperties.Should().BeFalse();
            var duckWithoutProperties = instanceWithoutProperties.DuckCast <ILogEventInfoProxy>();

            duckWithoutProperties.HasProperties.Should().BeFalse();

            instanceWithoutProperties.Properties["TestKey"] = "TestValue";
            duckWithoutProperties.HasProperties.Should().BeTrue();
            duckWithoutProperties.Properties.Should().ContainKey("TestKey").WhoseValue.Should().Be("TestValue");
        }
예제 #3
0
        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
        }