コード例 #1
0
        public void LogScope_WithOneParameter()
        {
            // Arrange
            var param1 = Guid.NewGuid().ToString();
            var testSink = new TestSink();
            var testLogger = new TestLogger("testlogger", testSink, enabled: true);

            // Act
            var disposable = testLogger.ScopeWithOneParam(param1);

            // Assert
            Assert.NotNull(disposable);
            Assert.Equal(0, testSink.Writes.Count);
            Assert.Equal(1, testSink.Scopes.Count);
            var scopeContext = testSink.Scopes.First();
            var actualLogValues = Assert.IsAssignableFrom<ILogValues>(scopeContext.Scope);
            AssertLogValues(new[]
            {
                new KeyValuePair<string, object>("RequestId", param1),
                new KeyValuePair<string, object>("{OriginalFormat}", TestLoggerExtensions.ScopeWithOneParameter.NamedStringFormat)
            },
            actualLogValues.GetValues());
            Assert.Equal(
                string.Format(TestLoggerExtensions.ScopeWithOneParameter.FormatString, param1),
                actualLogValues.ToString());
        }
コード例 #2
0
        public void LogMessage()
        {
            // Arrange
            var controller = "home";
            var action = "index";
            var testSink = new TestSink();
            var testLogger = new TestLogger("testlogger", testSink, enabled: true);

            // Act
            testLogger.ActionMatched(controller, action);

            // Assert
            Assert.Equal(1, testSink.Writes.Count);
            var writeContext = testSink.Writes.First();
            var actualLogValues = Assert.IsAssignableFrom<ILogValues>(writeContext.State);
            AssertLogValues(
                new[] {
                    new KeyValuePair<string, object>("{OriginalFormat}", TestLoggerExtensions.ActionMatchedInfo.NamedStringFormat),
                    new KeyValuePair<string, object>("controller", controller),
                    new KeyValuePair<string, object>("action", action)
                },
                actualLogValues.GetValues());
            Assert.Equal(LogLevel.Information, writeContext.LogLevel);
            Assert.Equal(1, writeContext.EventId);
            Assert.Null(writeContext.Exception);
            Assert.Equal(
                string.Format(
                    TestLoggerExtensions.ActionMatchedInfo.FormatString,
                    controller,
                    action),
                actualLogValues.ToString());
        }
コード例 #3
0
        public void LogScope_WithoutAnyParameters()
        {
            // Arrange
            var testSink = new TestSink();
            var testLogger = new TestLogger("testlogger", testSink, enabled: true);

            // Act
            var disposable = testLogger.ScopeWithoutAnyParams();

            // Assert
            Assert.NotNull(disposable);
            Assert.Equal(0, testSink.Writes.Count);
            Assert.Equal(1, testSink.Scopes.Count);
            var scopeContext = testSink.Scopes.First();
            var actualLogValues = Assert.IsAssignableFrom<ILogValues>(scopeContext.Scope);
            AssertLogValues(new[]
            {
                new KeyValuePair<string, object>("{OriginalFormat}", TestLoggerExtensions.ScopeWithoutAnyParameters.Message)
            },
            actualLogValues.GetValues());
            Assert.Equal(
                TestLoggerExtensions.ScopeWithoutAnyParameters.Message,
                actualLogValues.ToString());
        }
コード例 #4
0
        public void BeginScope_CreatesScope_WithFormatStringValues()
        {
            // Arrange
            var testSink = new TestSink(
                writeEnabled: (writeContext) => true, 
                beginEnabled: (beginScopeContext) => true);
            var logger = new TestLogger("TestLogger", testSink, enabled: true);
            var actionName = "App.Controllers.Home.Index";
            var expectedStringMessage = "Executing action " + actionName;

            // Act
            var scope = logger.BeginScope("Executing action {ActionName}", actionName);

            // Assert
            Assert.Equal(1, testSink.Scopes.Count);
            Assert.IsType<FormattedLogValues>(testSink.Scopes[0].Scope);
            var scopeState = (FormattedLogValues)testSink.Scopes[0].Scope;
            Assert.Equal(expectedStringMessage, scopeState.ToString());
            var scopeProperties = scopeState.GetValues();
            Assert.NotNull(scopeProperties);
            Assert.Contains(scopeProperties, (kvp) =>
            {
                return (string.Equals(kvp.Key, "ActionName") && string.Equals(kvp.Value?.ToString(), actionName));
            });
        }
コード例 #5
0
 private TestLogger SetUp(TestSink sink)
 {
     // Arrange
     var logger = new TestLogger(_name, sink, enabled: true);
     return logger;
 }
コード例 #6
0
        public void LogScope_WithThreeParameters()
        {
            // Arrange
            var param1 = "foo";
            var param2 = "bar";
            int param3 = 10;
            var testSink = new TestSink();
            var testLogger = new TestLogger("testlogger", testSink, enabled: true);

            // Act
            var disposable = testLogger.ScopeWithThreeParams(param1, param2, param3);

            // Assert
            Assert.NotNull(disposable);
            Assert.Equal(0, testSink.Writes.Count);
            Assert.Equal(1, testSink.Scopes.Count);
            var scopeContext = testSink.Scopes.First();
            var actualLogValues = Assert.IsAssignableFrom<ILogValues>(scopeContext.Scope);
            AssertLogValues(new[]
            {
                new KeyValuePair<string, object>("param1", param1),
                new KeyValuePair<string, object>("param2", param2),
                new KeyValuePair<string, object>("param3", param3),
                new KeyValuePair<string, object>("{OriginalFormat}", TestLoggerExtensions.ScopeInfoWithThreeParameters.NamedStringFormat)
            },
            actualLogValues.GetValues());
            Assert.Equal(
                string.Format(TestLoggerExtensions.ScopeInfoWithThreeParameters.FormatString, param1, param2, param3),
                actualLogValues.ToString());
        }
コード例 #7
0
 private TestLogger SetUp(TestSink sink)
 {
     // Arrange
     var logger = new TestLogger(_name, sink, enabled: true);
     return logger;
 }