Example #1
0
        public void EventIdWrittenToEventLogUsesDefaultIfSpecified()
        {
            // Arrange
            var loggerName      = "Test";
            var expectedMessage = "Category: " + loggerName + Environment.NewLine +
                                  "EventId: 1" + Environment.NewLine + Environment.NewLine +
                                  "Message" + Environment.NewLine;

            var testEventLog = new TestEventLog(expectedMessage.Length)
            {
                DefaultEventId = 1034
            };
            var logger = new EventLogLogger(loggerName, new EventLogSettings()
            {
                EventLog = testEventLog
            }, new LoggerExternalScopeProvider());

            // Act
            logger.LogInformation(new EventId(1, "FooEvent"), "Message");

            // Assert
            Assert.Single(testEventLog.Messages);
            Assert.Equal(expectedMessage, testEventLog.Messages[0]);
            Assert.Equal(1034, testEventLog.Entries[0].EventId);
        }
        public void Message_WritesFullMessageWithScopes()
        {
            // Arrange
            var loggerName      = "Test";
            var maxMessageSize  = 50 + loggerName.Length + Environment.NewLine.Length;
            var expectedMessage = loggerName + Environment.NewLine +
                                  "Message" + Environment.NewLine +
                                  "Outer Scope" + Environment.NewLine +
                                  "Inner Scope";
            var testEventLog = new TestEventLog(maxMessageSize);
            var logger       = new EventLogLogger(loggerName, new EventLogSettings()
            {
                EventLog = testEventLog
            });

            // Act
            using (logger.BeginScope("Outer Scope"))
                using (logger.BeginScope("Inner Scope"))
                {
                    logger.LogInformation("Message");
                }

            // Assert
            Assert.Single(testEventLog.Messages);
            Assert.Equal(expectedMessage, testEventLog.Messages[0]);
        }
Example #3
0
        public void Message_WritesFullMessageWithScopes()
        {
            // Arrange
            var loggerName      = "Test";
            var expectedMessage = "Category: " + loggerName + Environment.NewLine +
                                  "EventId: 0" + Environment.NewLine +
                                  "Outer Scope" + Environment.NewLine +
                                  "Inner Scope" + Environment.NewLine + Environment.NewLine +
                                  "Message" + Environment.NewLine;
            var testEventLog = new TestEventLog(expectedMessage.Length);
            var logger       = new EventLogLogger(loggerName, new EventLogSettings()
            {
                EventLog = testEventLog
            }, new LoggerExternalScopeProvider());

            // Act
            using (logger.BeginScope("Outer Scope"))
                using (logger.BeginScope("Inner Scope"))
                {
                    logger.LogInformation("Message");
                }

            // Assert
            Assert.Single(testEventLog.Messages);
            Assert.Equal(expectedMessage, testEventLog.Messages[0]);
        }
        public void MessageExceedingMaxSize_WritesSplitMessages(int messageSize, string[] expectedMessages)
        {
            // Arrange
            var loggerName     = "Test";
            var maxMessageSize = 10;
            var message        = new string('a', messageSize);
            var testEventLog   = new TestEventLog(maxMessageSize);
            var logger         = new EventLogLogger(loggerName, new EventLogSettings()
            {
                EventLog = testEventLog
            });

            // Act
            logger.LogInformation(message);

            // Assert
            Assert.Equal(expectedMessages.Length, testEventLog.Messages.Count);
            Assert.Equal(expectedMessages, testEventLog.Messages);
        }
        public void MessageWithinMaxSize_WritesFullMessage(int messageSize)
        {
            // Arrange
            var loggerName      = "Test";
            var maxMessageSize  = 50 + loggerName.Length + Environment.NewLine.Length;
            var message         = new string('a', messageSize);
            var expectedMessage = loggerName + Environment.NewLine + message;
            var testEventLog    = new TestEventLog(maxMessageSize);
            var logger          = new EventLogLogger(loggerName, new EventLogSettings()
            {
                EventLog = testEventLog
            });

            // Act
            logger.LogInformation(message);

            // Assert
            Assert.Single(testEventLog.Messages);
            Assert.Equal(expectedMessage, testEventLog.Messages[0]);
        }
Example #6
0
        public void MessageExceedingMaxSize_WritesSplitMessages(int messageSize, string[] expectedMessages)
        {
            // Arrange
            var headerLength   = "EventId: 0".Length + "Category: ".Length;
            var loggerName     = "Test";
            var maxMessageSize = headerLength + loggerName.Length + Environment.NewLine.Length * 3;
            var message        = new string('a', messageSize);
            var testEventLog   = new TestEventLog(maxMessageSize);
            var logger         = new EventLogLogger(loggerName, new EventLogSettings()
            {
                EventLog = testEventLog
            }, new LoggerExternalScopeProvider());

            // Act
            logger.LogInformation(message);

            // Assert
            Assert.Equal(expectedMessages.Length, testEventLog.Messages.Count);
            Assert.Equal(expectedMessages, testEventLog.Messages);
        }
Example #7
0
        public void Message_WritesFullMessageWithScopes()
        {
            // Arrange
            var loggerName      = "Test";
            var expectedMessage = "Category: " + loggerName + Environment.NewLine +
                                  "EventId: 0" + Environment.NewLine +
                                  "Outer Scope" + Environment.NewLine +
                                  "Inner Scope" + Environment.NewLine +
                                  "K1: V1" + Environment.NewLine +
                                  "K2: " + Environment.NewLine +
                                  "K3: " + Environment.NewLine +
                                  Environment.NewLine +
                                  "Message" + Environment.NewLine;
            var testEventLog = new TestEventLog(expectedMessage.Length);
            var logger       = new EventLogLogger(loggerName, new EventLogSettings()
            {
                EventLog = testEventLog
            }, new LoggerExternalScopeProvider());
            var scopeWithValues = new Dictionary <string, object>
            {
                { "K1", "V1" },
                { "K2", "" },
                { "K3", null },
            };

            // Act
            using (logger.BeginScope("Outer Scope"))
                using (logger.BeginScope("Inner Scope"))
                    using (logger.BeginScope(scopeWithValues))
                        using (logger.BeginScope((object)null))
                        {
                            logger.LogInformation("Message");
                        }

            // Assert
            Assert.Single(testEventLog.Messages);
            Assert.Equal(expectedMessage, testEventLog.Messages[0]);
        }
Example #8
0
        public void MessageWithinMaxSize_WritesFullMessage(int messageSize)
        {
            var headerLength = "EventId: 0".Length + "Category: ".Length;
            // Arrange
            var loggerName      = "Test";
            var maxMessageSize  = 50 + headerLength + loggerName.Length + Environment.NewLine.Length * 4;
            var message         = new string('a', messageSize);
            var expectedMessage = "Category: " + loggerName + Environment.NewLine +
                                  "EventId: 0" + Environment.NewLine + Environment.NewLine +
                                  message + Environment.NewLine;
            var testEventLog = new TestEventLog(maxMessageSize);
            var logger       = new EventLogLogger(loggerName, new EventLogSettings()
            {
                EventLog = testEventLog
            }, new LoggerExternalScopeProvider());

            // Act
            logger.LogInformation(message);

            // Assert
            Assert.Single(testEventLog.Messages);
            Assert.Equal(expectedMessage, testEventLog.Messages[0]);
        }