public void ShouldAddAHostNameProperty()
        {
            var evt      = _fixture.Create <LoggingEvent>();
            var instance = new LogdnaFormatter(new Config());

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var hostName = (string)json.hostName;

            hostName.Should().NotBeNullOrEmpty("because the machine name is used to set the hostname");
        }
        public void ShouldAddALoggerNameProperty()
        {
            var evt      = _fixture.Create <LoggingEvent>();
            var instance = new LogdnaFormatter(new Config());

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var loggerName = (string)json.loggerName;

            loggerName.Should().StartWith("LoggerName", "because this is the value of the LoggerName property on the event");
        }
        public void ShouldAddALevelProperty()
        {
            var evt      = _fixture.Create <LoggingEvent>();
            var instance = new LogdnaFormatter(new Config());

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var level = (string)json.level;

            level.Should().StartWith("levelName", "because the level name property on the event is used");
        }
        public void ShouldAddAValidTimestampProperty()
        {
            var evt      = _fixture.Create <LoggingEvent>();
            var instance = new LogdnaFormatter(new Config());

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var timestamp = (string)json.timestamp;

            timestamp.Should().NotBeNullOrEmpty("because the timestamp property should always be set");
            DateTime.TryParse(timestamp, out _).Should().BeTrue("because the timestamp should always be a valid date");
        }
        public void ShouldAddAMessagePropertyForEventsWithoutMessages()
        {
            var evt = new LoggingEvent(new LoggingEventData {
                Level = Level.Info
            });
            var instance = new LogdnaFormatter(new Config());

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var message = (string)json.message;

            message.Should()
            .Be("null", "because the MessageObject property is null but we want to log \"null\" to show this explicitly");
        }
        public void ShouldSerializeRenderedMessageIfNoMessageObjectIsSet()
        {
            var evt = new LoggingEvent(
                GetType(),
                CreateMockRepository(),
                new LoggingEventData
            {
                Message = "test message"
            });
            var instance = new LogdnaFormatter(new Config());

            var result = instance.ToJson(evt, evt.RenderedMessage);
            var json   = JObject.Parse(result);

            json.Should().HaveElement("message").Which.Should().HaveValue("test message");
        }
        public void ShouldSerializeFormattedString()
        {
            var evt = new LoggingEvent(
                GetType(),
                CreateMockRepository(),
                _fixture.Create("loggerName"),
                _fixture.Create <Level>(),
                new SystemStringFormat(CultureInfo.InvariantCulture, "test: {0}{1}{2}", 1, 2, 3),
                null);
            var instance = new LogdnaFormatter(new Config());

            var result = instance.ToJson(evt, evt.RenderedMessage);
            var json   = JObject.Parse(result);

            json.Should().HaveElement("message").Which.Should().HaveValue("test: 123");
        }
        public void ShouldSerializeCustomObjectToOutput()
        {
            var evt = new LoggingEvent(
                GetType(),
                CreateMockRepository(),
                _fixture.Create("loggerName"),
                _fixture.Create <Level>(),
                new { property1 = "value1" },
                null);
            var instance = new LogdnaFormatter(new Config());

            var result = instance.ToJson(evt, evt.RenderedMessage);
            var json   = JObject.Parse(result);

            json.Should().HaveElement("property1").Which.Should().HaveValue("value1");
        }
        public void ShouldSerializeGlobalContextEnvProperties()
        {
            var evt = _fixture.Create <LoggingEvent>();

            PopulateSixContextProperties(GlobalContext.Properties);

            var instance = new LogdnaFormatter(new Config
            {
                Env = "prod"
            });

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            VerifyContextPropertiesInJson(json);
        }
        public void ShouldSetMessagePropertyWhenMessageObjectIsString()
        {
            var evt = new LoggingEvent(
                GetType(),
                CreateMockRepository(),
                _fixture.Create("loggerName"),
                _fixture.Create <Level>(),
                _fixture.Create("message"),
                null);
            var instance = new LogdnaFormatter(new Config());

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var message = (string)json.message;

            message.Should().StartWith("message", "because the MessageObject property value is used");
        }
        public void ShouldAddExtraPropertiesWhenMessageObjectIsAComplexType()
        {
            var evt = new LoggingEvent(
                GetType(),
                null,
                _fixture.Create("loggerName"),
                _fixture.Create <Level>(),
                _fixture.Create <ComplexType>(),
                null);
            var instance = new LogdnaFormatter(new Config());

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var propertyOne = (string)json.PropertyOne;
            var propertyTwo = (int)json.PropertyTwo;

            propertyOne.Should().StartWith("PropertyOne", "because the value from the PropertyOne property on the complex type is used");
            propertyTwo.Should().BeGreaterThan(0, "because the value of the PropertyTwo property on the complex type is used");
        }
        public void EventWithoutMessageExceedingMaxSizeIsDropped()
        {
            var config = new Config
            {
                // there is 155 bytes of fixed JSON (for "evt" values below) unrelated to "message" value
                // so this value should cut message to first 20 characters
                MaxEventSizeBytes = 175
            };
            var formatter = new LogdnaFormatter(config);
            var evt       = new LoggingEvent(
                GetType(),
                CreateMockRepository(),
                "logger",
                Level.Info,
                new { Property1 = "value1", Property2 = "value2", Property3 = "value3" },
                null);

            var result = formatter.ToJson(evt, "<anonymous_type>");

            result.Should().BeNull();
        }
        public void ShouldSerializeInnerExceptions(int configurationNumberOfInnerExceptions, int expectedNumberOfInnerExceptions, int innerExceptionsToCreate)
        {
            Exception ex = GetArgumentException(innerExceptionsToCreate + 1);

            var evt = new LoggingEvent(
                GetType(),
                CreateMockRepository(),
                _fixture.Create("loggerName"),
                _fixture.Create <Level>(),
                _fixture.Create("message"),
                ex);
            var instance = new LogdnaFormatter(new Config
            {
                NumberOfInnerExceptions = configurationNumberOfInnerExceptions
            });

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var exception = json.exception;

            ((object)exception).Should().NotBeNull("because an exception was specified in the event");

            var count = 0;

            while (exception != null)
            {
                var message    = (string)exception.exceptionMessage;
                var type       = (string)exception.exceptionType;
                var stacktrace = (string)exception.stacktrace;
                AssertException(message, type, stacktrace, count);
                exception = exception.innerException;
                if (exception != null)
                {
                    count++;
                }
            }

            count.Should().Be(expectedNumberOfInnerExceptions, "Expects all stacktraces");
        }
        public void ShouldSerializeTheException()
        {
            // In order to populate the stacktrace.
            Exception ex;

            try
            {
                throw new ArgumentException();
            }
            catch (Exception e)
            {
                ex = e;
            }

            var evt = new LoggingEvent(
                GetType(),
                CreateMockRepository(),
                _fixture.Create("loggerName"),
                _fixture.Create <Level>(),
                _fixture.Create("message"),
                ex);
            var instance = new LogdnaFormatter(new Config());

            var     result = instance.ToJson(evt, evt.RenderedMessage);
            dynamic json   = JObject.Parse(result);

            var exception = json.exception;

            ((object)exception).Should().NotBeNull("because an exception was specified in the event");

            var message    = (string)exception.exceptionMessage;
            var type       = (string)exception.exceptionType;
            var stacktrace = (string)exception.stacktrace;

            message.Should().NotBeNullOrEmpty("because an argument exception has a default message");
            type.Should().Be(typeof(ArgumentException).FullName, "because we logged an argument exception");
            stacktrace.Should().NotBeNull("because the exception has a stacktrace");
        }