From 0f5bbdf41701e2396d2ca12808874c5b9afb1529 Mon Sep 17 00:00:00 2001 From: SonjaKhan Date: Wed, 8 Oct 2014 12:15:39 -0700 Subject: [PATCH] updating ILogger, see aspnet/Logging#3 --- Mvc.sln | 2 +- .../Logging/LogFormatter.cs | 2 +- .../Logging/LoggerExtensions.cs | 4 +-- .../DefaultActionSelectorTests.cs | 30 ++++--------------- .../Logging/NullLogger.cs | 6 +++- .../Logging/TestLogger.cs | 7 +++-- .../Logging/TestSink.cs | 12 ++++---- .../{WriteCoreContext.cs => WriteContext.cs} | 2 +- .../MvcRouteHandlerTests.cs | 30 ++++--------------- .../Routing/AttributeRouteTests.cs | 20 +++---------- .../NullLoggerFactory.cs | 6 +++- 11 files changed, 42 insertions(+), 79 deletions(-) rename test/Microsoft.AspNet.Mvc.Core.Test/Logging/{WriteCoreContext.cs => WriteContext.cs} (94%) diff --git a/Mvc.sln b/Mvc.sln index 7750fe6354..9fc74b2893 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.22013.1 +VisualStudioVersion = 14.0.22115.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}" EndProject diff --git a/src/Microsoft.AspNet.Mvc.Core/Logging/LogFormatter.cs b/src/Microsoft.AspNet.Mvc.Core/Logging/LogFormatter.cs index ac280abfb7..45fee7a5fe 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Logging/LogFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Logging/LogFormatter.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Mvc.Logging public static class LogFormatter { /// - /// A formatter for use with (write.State); @@ -96,15 +90,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(typeof(DefaultActionSelector).FullName, scope.LoggerName); Assert.Equal("DefaultActionSelector.SelectAsync", scope.Scope); - // There is a record for IsEnabled and one for WriteCore. - Assert.Equal(2, sink.Writes.Count); + Assert.Equal(1, sink.Writes.Count); - var enabled = sink.Writes[0]; - Assert.Equal(typeof(DefaultActionSelector).FullName, enabled.LoggerName); - Assert.Equal("DefaultActionSelector.SelectAsync", enabled.Scope); - Assert.Null(enabled.State); - - var write = sink.Writes[1]; + var write = sink.Writes[0]; Assert.Equal(typeof(DefaultActionSelector).FullName, write.LoggerName); Assert.Equal("DefaultActionSelector.SelectAsync", write.Scope); var values = Assert.IsType(write.State); @@ -144,15 +132,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(typeof(DefaultActionSelector).FullName, scope.LoggerName); Assert.Equal("DefaultActionSelector.SelectAsync", scope.Scope); - // There is a record for IsEnabled and one for WriteCore. - Assert.Equal(2, sink.Writes.Count); + Assert.Equal(1, sink.Writes.Count); - var enabled = sink.Writes[0]; - Assert.Equal(typeof(DefaultActionSelector).FullName, enabled.LoggerName); - Assert.Equal("DefaultActionSelector.SelectAsync", enabled.Scope); - Assert.Null(enabled.State); - - var write = sink.Writes[1]; + var write = sink.Writes[0]; Assert.Equal(typeof(DefaultActionSelector).FullName, write.LoggerName); Assert.Equal("DefaultActionSelector.SelectAsync", write.Scope); var values = Assert.IsType(write.State); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Logging/NullLogger.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Logging/NullLogger.cs index af228397f8..17c68b7981 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Logging/NullLogger.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Logging/NullLogger.cs @@ -15,7 +15,11 @@ namespace Microsoft.AspNet.Mvc return NullDisposable.Instance; } - public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func formatter) + public void Write(TraceType eventType, int eventId, object state, Exception exception, Func formatter) + { + } + + public bool IsEnabled(TraceType eventType) { return false; } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Logging/TestLogger.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Logging/TestLogger.cs index 8bf8301ae4..68110ae682 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Logging/TestLogger.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Logging/TestLogger.cs @@ -33,9 +33,9 @@ namespace Microsoft.AspNet.Mvc return NullDisposable.Instance; } - public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func formatter) + public void Write(TraceType eventType, int eventId, object state, Exception exception, Func formatter) { - _sink.Write(new WriteCoreContext() + _sink.Write(new WriteContext() { EventType = eventType, EventId = eventId, @@ -45,7 +45,10 @@ namespace Microsoft.AspNet.Mvc LoggerName = _name, Scope = _scope }); + } + public bool IsEnabled(TraceType eventType) + { return true; } } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Logging/TestSink.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Logging/TestSink.cs index 29d48c3143..eefbb1c099 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Logging/TestSink.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Logging/TestSink.cs @@ -9,25 +9,25 @@ namespace Microsoft.AspNet.Mvc public class TestSink { public TestSink( - Func writeEnabled = null, + Func writeEnabled = null, Func beginEnabled = null) { WriteEnabled = writeEnabled; BeginEnabled = beginEnabled; Scopes = new List(); - Writes = new List(); + Writes = new List(); } - public Func WriteEnabled { get; set; } + public Func WriteEnabled { get; set; } public Func BeginEnabled { get; set; } public List Scopes { get; set; } - public List Writes { get; set; } + public List Writes { get; set; } - public void Write(WriteCoreContext context) + public void Write(WriteContext context) { if (WriteEnabled == null || WriteEnabled(context)) { @@ -43,7 +43,7 @@ namespace Microsoft.AspNet.Mvc } } - public static bool EnableWithTypeName(WriteCoreContext context) + public static bool EnableWithTypeName(WriteContext context) { return context.LoggerName.Equals(typeof(T).FullName); } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Logging/WriteCoreContext.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Logging/WriteContext.cs similarity index 94% rename from test/Microsoft.AspNet.Mvc.Core.Test/Logging/WriteCoreContext.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/Logging/WriteContext.cs index 838aac7af8..a8b0ff291c 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Logging/WriteCoreContext.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Logging/WriteContext.cs @@ -6,7 +6,7 @@ using Microsoft.Framework.Logging; namespace Microsoft.AspNet.Mvc { - public class WriteCoreContext + public class WriteContext { public TraceType EventType { get; set; } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/MvcRouteHandlerTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/MvcRouteHandlerTests.cs index 14e5edf4b3..a814ae0ae3 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/MvcRouteHandlerTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/MvcRouteHandlerTests.cs @@ -38,15 +38,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName); Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope); - // There is a record for IsEnabled and one for WriteCore. - Assert.Equal(2, sink.Writes.Count); + Assert.Equal(1, sink.Writes.Count); - var enabled = sink.Writes[0]; - Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName); - Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope); - Assert.Null(enabled.State); - - var write = sink.Writes[1]; + var write = sink.Writes[0]; Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName); Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope); var values = Assert.IsType(write.State); @@ -82,15 +76,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName); Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope); - // There is a record for IsEnabled and one for WriteCore. - Assert.Equal(2, sink.Writes.Count); + Assert.Equal(1, sink.Writes.Count); - var enabled = sink.Writes[0]; - Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName); - Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope); - Assert.Null(enabled.State); - - var write = sink.Writes[1]; + var write = sink.Writes[0]; Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName); Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope); var values = Assert.IsType(write.State); @@ -127,15 +115,9 @@ namespace Microsoft.AspNet.Mvc Assert.Equal(typeof(MvcRouteHandler).FullName, scope.LoggerName); Assert.Equal("MvcRouteHandler.RouteAsync", scope.Scope); - // There is a record for IsEnabled and one for WriteCore. - Assert.Equal(2, sink.Writes.Count); + Assert.Equal(1, sink.Writes.Count); - var enabled = sink.Writes[0]; - Assert.Equal(typeof(MvcRouteHandler).FullName, enabled.LoggerName); - Assert.Equal("MvcRouteHandler.RouteAsync", enabled.Scope); - Assert.Null(enabled.State); - - var write = sink.Writes[1]; + var write = sink.Writes[0]; Assert.Equal(typeof(MvcRouteHandler).FullName, write.LoggerName); Assert.Equal("MvcRouteHandler.RouteAsync", write.Scope); var values = Assert.IsType(write.State); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRouteTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRouteTests.cs index 53c9bb48c0..d637a71e3a 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRouteTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Routing/AttributeRouteTests.cs @@ -682,15 +682,9 @@ namespace Microsoft.AspNet.Mvc.Routing Assert.Equal(typeof(AttributeRoute).FullName, scope.LoggerName); Assert.Equal("AttributeRoute.RouteAsync", scope.Scope); - // There is a record for IsEnabled and one for WriteCore. - Assert.Equal(2, sink.Writes.Count); + Assert.Equal(1, sink.Writes.Count); - var enabled = sink.Writes[0]; - Assert.Equal(typeof(AttributeRoute).FullName, enabled.LoggerName); - Assert.Equal("AttributeRoute.RouteAsync", enabled.Scope); - Assert.Null(enabled.State); - - var write = sink.Writes[1]; + var write = sink.Writes[0]; Assert.Equal(typeof(AttributeRoute).FullName, write.LoggerName); Assert.Equal("AttributeRoute.RouteAsync", write.Scope); var values = Assert.IsType(write.State); @@ -719,15 +713,9 @@ namespace Microsoft.AspNet.Mvc.Routing Assert.Equal(typeof(AttributeRoute).FullName, scope.LoggerName); Assert.Equal("AttributeRoute.RouteAsync", scope.Scope); - // There is a record for IsEnabled and one for WriteCore. - Assert.Equal(2, sink.Writes.Count); + Assert.Equal(1, sink.Writes.Count); - var enabled = sink.Writes[0]; - Assert.Equal(typeof(AttributeRoute).FullName, enabled.LoggerName); - Assert.Equal("AttributeRoute.RouteAsync", enabled.Scope); - Assert.Null(enabled.State); - - var write = sink.Writes[1]; + var write = sink.Writes[0]; Assert.Equal(typeof(AttributeRoute).FullName, write.LoggerName); Assert.Equal("AttributeRoute.RouteAsync", write.Scope); var values = Assert.IsType(write.State); diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/NullLoggerFactory.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/NullLoggerFactory.cs index a5a0462970..02930abcf3 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/NullLoggerFactory.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/NullLoggerFactory.cs @@ -27,7 +27,11 @@ namespace Microsoft.AspNet.Mvc return NullDisposable.Instance; } - public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func formatter) + public void Write(TraceType eventType, int eventId, object state, Exception exception, Func formatter) + { + } + + public bool IsEnabled(TraceType eventType) { return false; }