Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit 2a8ac59

Browse files
committed
Refactoring ILogger, see aspnet/Logging#3
1 parent 3ff9d6e commit 2a8ac59

File tree

11 files changed

+323
-200
lines changed

11 files changed

+323
-200
lines changed

Routing.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.22013.1
4+
VisualStudioVersion = 14.0.22115.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0E966C37-7334-4D96-AAF6-9F49FBD166E3}"
77
EndProject

src/Microsoft.AspNet.Routing/Logging/LoggerExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Microsoft.AspNet.Routing.Logging
77
{
88
internal static class LoggerExtensions
99
{
10-
public static bool WriteValues([NotNull] this ILogger logger, object values)
10+
public static void WriteValues([NotNull] this ILogger logger, object values)
1111
{
12-
return logger.WriteCore(
12+
logger.Write(
1313
eventType: TraceType.Information,
1414
eventId: 0,
1515
state: values,

test/Microsoft.AspNet.Routing.Tests/ConstraintMatcherTest.cs

Lines changed: 79 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ namespace Microsoft.AspNet.Routing
1414
public class ConstraintMatcherTest
1515
{
1616
#if ASPNET50
17+
private const string _name = "name";
18+
1719
[Fact]
1820
public void MatchUrlGeneration_DoesNotLogData()
1921
{
2022
// Arrange
21-
var name = "name";
22-
2323
var sink = new TestSink();
24-
var logger = new TestLogger(name, sink);
24+
var logger = new TestLogger(_name, sink, enabled: true);
2525

2626
var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" });
2727
var constraints = new Dictionary<string, IRouteConstraint>
@@ -41,62 +41,40 @@ public void MatchUrlGeneration_DoesNotLogData()
4141

4242
// Assert
4343
// There are no BeginScopes called.
44-
Assert.Equal(0, sink.Scopes.Count);
44+
Assert.Empty(sink.Scopes);
4545

4646
// There are no WriteCores called.
47-
Assert.Equal(0, sink.Writes.Count);
47+
Assert.Empty(sink.Writes);
4848
}
4949

5050
[Fact]
5151
public void MatchFail_LogsCorrectData()
5252
{
53-
// Arrange
54-
var name = "name";
55-
56-
var sink = new TestSink();
57-
var logger = new TestLogger(name, sink);
58-
59-
var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" });
53+
// Arrange & Act
6054
var constraints = new Dictionary<string, IRouteConstraint>
6155
{
6256
{"a", new PassConstraint()},
6357
{"b", new FailConstraint()}
6458
};
65-
66-
// Act
67-
RouteConstraintMatcher.Match(
68-
constraints: constraints,
69-
routeValues: routeValueDictionary,
70-
httpContext: new Mock<HttpContext>().Object,
71-
route: new Mock<IRouter>().Object,
72-
routeDirection: RouteDirection.IncomingRequest,
73-
logger: logger);
59+
var sink = SetUpMatch(constraints, true);
7460

7561
// Assert
7662
// There are no begin scopes called.
77-
Assert.Equal(0, sink.Scopes.Count);
78-
79-
// There are two records for IsEnabled and two for WriteCore.
80-
Assert.Equal(4, sink.Writes.Count);
63+
Assert.Empty(sink.Scopes);
8164

82-
var enabled = sink.Writes[0];
83-
Assert.Equal(name, enabled.LoggerName);
84-
Assert.Null(enabled.State);
65+
// There are two records for WriteCore.
66+
Assert.Equal(2, sink.Writes.Count);
8567

86-
var write = sink.Writes[1];
87-
Assert.Equal(name, write.LoggerName);
68+
var write = sink.Writes[0];
69+
Assert.Equal(_name, write.LoggerName);
8870
var values = Assert.IsType<RouteConstraintMatcherMatchValues>(write.State);
8971
Assert.Equal("RouteConstraintMatcher.Match", values.Name);
9072
Assert.Equal("a", values.ConstraintKey);
9173
Assert.Equal(constraints["a"], values.Constraint);
9274
Assert.Equal(true, values.Matched);
9375

94-
enabled = sink.Writes[2];
95-
Assert.Equal(name, enabled.LoggerName);
96-
Assert.Null(enabled.State);
97-
98-
write = sink.Writes[3];
99-
Assert.Equal(name, write.LoggerName);
76+
write = sink.Writes[1];
77+
Assert.Equal(_name, write.LoggerName);
10078
values = Assert.IsType<RouteConstraintMatcherMatchValues>(write.State);
10179
Assert.Equal("RouteConstraintMatcher.Match", values.Name);
10280
Assert.Equal("b", values.ConstraintKey);
@@ -105,62 +83,78 @@ public void MatchFail_LogsCorrectData()
10583
}
10684

10785
[Fact]
108-
public void MatchSuccess_LogsCorrectData()
86+
public void MatchFail_DisabledLoggerDoesNotLog()
10987
{
110-
// Arrange
111-
var name = "name";
88+
// Arrange & Act
89+
var constraints = new Dictionary<string, IRouteConstraint>
90+
{
91+
{"a", new PassConstraint()},
92+
{"b", new FailConstraint()}
93+
};
94+
var sink = SetUpMatch(constraints, false);
11295

113-
var sink = new TestSink();
114-
var logger = new TestLogger(name, sink);
96+
// Assert
97+
// There are no begin scopes called.
98+
Assert.Empty(sink.Scopes);
11599

116-
var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" });
100+
// Logger is disabled so it should not write
101+
Assert.Empty(sink.Writes);
102+
}
103+
104+
[Fact]
105+
public void MatchSuccess_LogsCorrectData()
106+
{
107+
// Arrange & Act
117108
var constraints = new Dictionary<string, IRouteConstraint>
118109
{
119110
{"a", new PassConstraint()},
120111
{"b", new PassConstraint()}
121112
};
122-
123-
// Act
124-
RouteConstraintMatcher.Match(
125-
constraints: constraints,
126-
routeValues: routeValueDictionary,
127-
httpContext: new Mock<HttpContext>().Object,
128-
route: new Mock<IRouter>().Object,
129-
routeDirection: RouteDirection.IncomingRequest,
130-
logger: logger);
113+
var sink = SetUpMatch(constraints, true);
131114

132115
// Assert
133116
// There are no begin scopes called.
134-
Assert.Equal(0, sink.Scopes.Count);
135-
136-
// There are two records for IsEnabled and two for WriteCore.
137-
Assert.Equal(4, sink.Writes.Count);
117+
Assert.Empty(sink.Scopes);
138118

139-
var enabled = sink.Writes[0];
140-
Assert.Equal(name, enabled.LoggerName);
141-
Assert.Null(enabled.State);
119+
// There are two records WriteCore.
120+
Assert.Equal(2, sink.Writes.Count);
142121

143-
var write = sink.Writes[1];
144-
Assert.Equal(name, write.LoggerName);
122+
var write = sink.Writes[0];
123+
Assert.Equal(_name, write.LoggerName);
145124
var values = Assert.IsType<RouteConstraintMatcherMatchValues>(write.State);
146125
Assert.Equal("RouteConstraintMatcher.Match", values.Name);
147126
Assert.Equal("a", values.ConstraintKey);
148127
Assert.Equal(constraints["a"], values.Constraint);
149128
Assert.Equal(true, values.Matched);
150129

151-
enabled = sink.Writes[2];
152-
Assert.Equal(name, enabled.LoggerName);
153-
Assert.Null(enabled.State);
154-
155-
write = sink.Writes[3];
156-
Assert.Equal(name, write.LoggerName);
130+
write = sink.Writes[1];
131+
Assert.Equal(_name, write.LoggerName);
157132
values = Assert.IsType<RouteConstraintMatcherMatchValues>(write.State);
158133
Assert.Equal("RouteConstraintMatcher.Match", values.Name);
159134
Assert.Equal("b", values.ConstraintKey);
160135
Assert.Equal(constraints["b"], values.Constraint);
161136
Assert.Equal(true, values.Matched);
162137
}
163138

139+
[Fact]
140+
public void MatchSuccess_DisabledLoggerDoesNotLog()
141+
{
142+
// Arrange & Act
143+
var constraints = new Dictionary<string, IRouteConstraint>
144+
{
145+
{"a", new PassConstraint()},
146+
{"b", new PassConstraint()}
147+
};
148+
var sink = SetUpMatch(constraints, false);
149+
150+
// Assert
151+
// There are no begin scopes called.
152+
Assert.Empty(sink.Scopes);
153+
154+
// Disabled Logger should not write
155+
Assert.Empty(sink.Writes);
156+
}
157+
164158
[Fact]
165159
public void ReturnsTrueOnValidConstraints()
166160
{
@@ -272,6 +266,25 @@ public void ReturnsTrueOnNullInput()
272266
routeDirection: RouteDirection.IncomingRequest,
273267
logger: NullLogger.Instance));
274268
}
269+
270+
private TestSink SetUpMatch(Dictionary<string, IRouteConstraint> constraints, bool enabled)
271+
{
272+
// Arrange
273+
var sink = new TestSink();
274+
var logger = new TestLogger(_name, sink, enabled);
275+
276+
var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" });
277+
278+
// Act
279+
RouteConstraintMatcher.Match(
280+
constraints: constraints,
281+
routeValues: routeValueDictionary,
282+
httpContext: new Mock<HttpContext>().Object,
283+
route: new Mock<IRouter>().Object,
284+
routeDirection: RouteDirection.IncomingRequest,
285+
logger: logger);
286+
return sink;
287+
}
275288
#endif
276289

277290
private class PassConstraint : IRouteConstraint

test/Microsoft.AspNet.Routing.Tests/Logging/NullLogger.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ public IDisposable BeginScope(object state)
1515
return NullDisposable.Instance;
1616
}
1717

18-
public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
18+
public void Write(TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
19+
{
20+
}
21+
22+
public bool IsEnabled(TraceType eventType)
1923
{
2024
return false;
2125
}

test/Microsoft.AspNet.Routing.Tests/Logging/TestLogger.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ public class TestLogger : ILogger
1111
private object _scope;
1212
private TestSink _sink;
1313
private string _name;
14+
private bool _enabled;
1415

15-
public TestLogger(string name, TestSink sink)
16-
{
16+
public TestLogger(string name, TestSink sink, bool enabled)
17+
{
1718
_sink = sink;
1819
_name = name;
19-
}
20+
_enabled = enabled;
21+
}
2022

2123
public string Name { get; set; }
2224

@@ -33,9 +35,9 @@ public IDisposable BeginScope(object state)
3335
return NullDisposable.Instance;
3436
}
3537

36-
public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
38+
public void Write(TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
3739
{
38-
_sink.Write(new WriteCoreContext()
40+
_sink.Write(new WriteContext()
3941
{
4042
EventType = eventType,
4143
EventId = eventId,
@@ -45,8 +47,11 @@ public bool WriteCore(TraceType eventType, int eventId, object state, Exception
4547
LoggerName = _name,
4648
Scope = _scope
4749
});
50+
}
4851

49-
return true;
52+
public bool IsEnabled(TraceType eventType)
53+
{
54+
return _enabled;
5055
}
5156
}
5257
}

test/Microsoft.AspNet.Routing.Tests/Logging/TestLoggerFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ namespace Microsoft.AspNet.Routing
88
public class TestLoggerFactory : ILoggerFactory
99
{
1010
private TestSink _sink;
11+
private bool _enabled;
1112

12-
public TestLoggerFactory(TestSink sink)
13+
public TestLoggerFactory(TestSink sink, bool enabled)
1314
{
1415
_sink = sink;
16+
_enabled = enabled;
1517
}
1618

1719
public ILogger Create(string name)
1820
{
19-
return new TestLogger(name, _sink);
21+
return new TestLogger(name, _sink, _enabled);
2022
}
2123

2224
public void AddProvider(ILoggerProvider provider)

test/Microsoft.AspNet.Routing.Tests/Logging/TestSink.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ namespace Microsoft.AspNet.Routing
99
public class TestSink
1010
{
1111
public TestSink(
12-
Func<WriteCoreContext, bool> writeEnabled = null,
12+
Func<WriteContext, bool> writeEnabled = null,
1313
Func<BeginScopeContext, bool> beginEnabled = null)
1414
{
1515
WriteEnabled = writeEnabled;
1616
BeginEnabled = beginEnabled;
1717

1818
Scopes = new List<BeginScopeContext>();
19-
Writes = new List<WriteCoreContext>();
19+
Writes = new List<WriteContext>();
2020
}
2121

22-
public Func<WriteCoreContext, bool> WriteEnabled { get; set; }
22+
public Func<WriteContext, bool> WriteEnabled { get; set; }
2323

2424
public Func<BeginScopeContext, bool> BeginEnabled { get; set; }
2525

2626
public List<BeginScopeContext> Scopes { get; set; }
2727

28-
public List<WriteCoreContext> Writes { get; set; }
28+
public List<WriteContext> Writes { get; set; }
2929

30-
public void Write(WriteCoreContext context)
30+
public void Write(WriteContext context)
3131
{
3232
if (WriteEnabled == null || WriteEnabled(context))
3333
{
@@ -43,7 +43,7 @@ public void Begin(BeginScopeContext context)
4343
}
4444
}
4545

46-
public static bool EnableWithTypeName<T>(WriteCoreContext context)
46+
public static bool EnableWithTypeName<T>(WriteContext context)
4747
{
4848
return context.LoggerName.Equals(typeof(T).FullName);
4949
}

test/Microsoft.AspNet.Routing.Tests/Logging/WriteCoreContext.cs renamed to test/Microsoft.AspNet.Routing.Tests/Logging/WriteContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Microsoft.AspNet.Routing
88
{
9-
public class WriteCoreContext
9+
public class WriteContext
1010
{
1111
public TraceType EventType { get; set; }
1212

0 commit comments

Comments
 (0)