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]);
        }
示例#2
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 CallingBeginScopeOnLogger_ReturnsNonNullableInstance()
        {
            // Arrange
            var logger = new EventLogLogger("Test", new EventLogSettings());

            // Act
            var disposable = logger.BeginScope("Scope1");

            // Assert
            Assert.NotNull(disposable);
        }
示例#4
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]);
        }