From e7728dde3f98cf02b0951ab8f1ad9108e337b06b Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Thu, 9 Apr 2015 15:50:31 -0700 Subject: [PATCH] Log view discovery --- .../ActionResults/PartialViewResult.cs | 18 +++++++++++--- .../ActionResults/ViewResult.cs | 18 +++++++++++--- .../ActionResults/PartialViewResultTest.cs | 21 +++++++++++++--- .../ActionResults/ViewResultTest.cs | 24 +++++++++++++++---- 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/PartialViewResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/PartialViewResult.cs index 3c60ff4a7c..ed48d986bf 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/PartialViewResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/PartialViewResult.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Internal; +using Microsoft.Framework.Logging; namespace Microsoft.AspNet.Mvc { @@ -50,10 +51,21 @@ namespace Microsoft.AspNet.Mvc var viewEngine = ViewEngine ?? context.HttpContext.RequestServices.GetRequiredService(); + var logger = context.HttpContext.RequestServices.GetRequiredService>(); + var viewName = ViewName ?? context.ActionDescriptor.Name; - var view = viewEngine.FindPartialView(context, viewName) - .EnsureSuccessful() - .View; + var viewEngineResult = viewEngine.FindPartialView(context, viewName); + if (!viewEngineResult.Success) + { + logger.LogError( + "The partial view '{PartialViewName}' was not found. Searched locations: {SearchedViewLocations}", + viewName, + viewEngineResult.SearchedLocations); + } + + var view = viewEngineResult.EnsureSuccessful().View; + + logger.LogVerbose("The partial view '{PartialViewName}' was found.", viewName); if (StatusCode != null) { diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs index e51064ab7a..96c6aefe5c 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Internal; +using Microsoft.Framework.Logging; namespace Microsoft.AspNet.Mvc { @@ -50,10 +51,21 @@ namespace Microsoft.AspNet.Mvc var viewEngine = ViewEngine ?? context.HttpContext.RequestServices.GetRequiredService(); + var logger = context.HttpContext.RequestServices.GetRequiredService>(); + var viewName = ViewName ?? context.ActionDescriptor.Name; - var view = viewEngine.FindView(context, viewName) - .EnsureSuccessful() - .View; + var viewEngineResult = viewEngine.FindView(context, viewName); + if(!viewEngineResult.Success) + { + logger.LogError( + "The view '{ViewName}' was not found. Searched locations: {SearchedViewLocations}", + viewName, + viewEngineResult.SearchedLocations); + } + + var view = viewEngineResult.EnsureSuccessful().View; + + logger.LogVerbose("The view '{ViewName}' was found.", viewName); if (StatusCode != null) { diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/PartialViewResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/PartialViewResultTest.cs index 118b85ad74..48e50abed0 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/PartialViewResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/PartialViewResultTest.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Routing; +using Microsoft.Framework.Logging; using Moq; using Xunit; @@ -21,7 +22,7 @@ namespace Microsoft.AspNet.Mvc "The view 'MyView' was not found. The following locations were searched:", "Location1", "Location2."); - var actionContext = new ActionContext(new DefaultHttpContext(), + var actionContext = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor()); var viewEngine = new Mock(); @@ -47,7 +48,7 @@ namespace Microsoft.AspNet.Mvc { // Arrange var viewName = "myview"; - var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor()); + var context = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor()); var viewEngine = new Mock(); var view = Mock.Of(); @@ -73,7 +74,7 @@ namespace Microsoft.AspNet.Mvc { // Arrange var viewName = "some-view-name"; - var context = new ActionContext(new DefaultHttpContext(), + var context = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor { Name = viewName }); var viewEngine = new Mock(); @@ -109,6 +110,8 @@ namespace Microsoft.AspNet.Mvc var serviceProvider = new Mock(); serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine))) .Returns(viewEngine.Object); + serviceProvider.Setup(p => p.GetService(typeof(ILogger))) + .Returns(new Mock>().Object); context.HttpContext.RequestServices = serviceProvider.Object; var viewResult = new PartialViewResult @@ -122,5 +125,17 @@ namespace Microsoft.AspNet.Mvc // Assert viewEngine.Verify(); } + + private HttpContext GetHttpContext() + { + var serviceProvider = new Mock(); + serviceProvider.Setup(s => s.GetService(typeof(ILogger))) + .Returns(new Mock>().Object); + + var httpContext = new DefaultHttpContext(); + httpContext.RequestServices = serviceProvider.Object; + + return httpContext; + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ViewResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ViewResultTest.cs index 01842b7547..919dfe0d5f 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ViewResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ViewResultTest.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Routing; +using Microsoft.Framework.Logging; using Moq; using Xunit; @@ -21,7 +22,8 @@ namespace Microsoft.AspNet.Mvc "The view 'MyView' was not found. The following locations were searched:", "Location1", "Location2."); - var actionContext = new ActionContext(new DefaultHttpContext(), + + var actionContext = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor()); var viewEngine = new Mock(); @@ -47,7 +49,7 @@ namespace Microsoft.AspNet.Mvc { // Arrange var viewName = "myview"; - var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor()); + var context = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor()); var viewEngine = new Mock(); var view = Mock.Of(); @@ -73,7 +75,7 @@ namespace Microsoft.AspNet.Mvc { // Arrange var viewName = "some-view-name"; - var context = new ActionContext(new DefaultHttpContext(), + var context = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor { Name = viewName }); var viewEngine = new Mock(); @@ -94,7 +96,7 @@ namespace Microsoft.AspNet.Mvc } [Fact] - public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified() + public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified() { // Arrange var viewName = "some-view-name"; @@ -109,6 +111,8 @@ namespace Microsoft.AspNet.Mvc var serviceProvider = new Mock(); serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine))) .Returns(viewEngine.Object); + serviceProvider.Setup(p => p.GetService(typeof(ILogger))) + .Returns(new Mock>().Object); context.HttpContext.RequestServices = serviceProvider.Object; var viewResult = new ViewResult @@ -122,5 +126,17 @@ namespace Microsoft.AspNet.Mvc // Assert viewEngine.Verify(); } + + private HttpContext GetHttpContext() + { + var serviceProvider = new Mock(); + serviceProvider.Setup(s => s.GetService(typeof(ILogger))) + .Returns(new Mock>().Object); + + var httpContext = new DefaultHttpContext(); + httpContext.RequestServices = serviceProvider.Object; + + return httpContext; + } } } \ No newline at end of file