Log view discovery

This commit is contained in:
Kiran Challa 2015-04-09 15:50:31 -07:00
parent 18efefd5cf
commit e7728dde3f
4 changed files with 68 additions and 13 deletions

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc
{ {
@ -50,10 +51,21 @@ namespace Microsoft.AspNet.Mvc
var viewEngine = ViewEngine ?? var viewEngine = ViewEngine ??
context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>(); context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>();
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<PartialViewResult>>();
var viewName = ViewName ?? context.ActionDescriptor.Name; var viewName = ViewName ?? context.ActionDescriptor.Name;
var view = viewEngine.FindPartialView(context, viewName) var viewEngineResult = viewEngine.FindPartialView(context, viewName);
.EnsureSuccessful() if (!viewEngineResult.Success)
.View; {
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) if (StatusCode != null)
{ {

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc
{ {
@ -50,10 +51,21 @@ namespace Microsoft.AspNet.Mvc
var viewEngine = ViewEngine ?? var viewEngine = ViewEngine ??
context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>(); context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>();
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<ViewResult>>();
var viewName = ViewName ?? context.ActionDescriptor.Name; var viewName = ViewName ?? context.ActionDescriptor.Name;
var view = viewEngine.FindView(context, viewName) var viewEngineResult = viewEngine.FindView(context, viewName);
.EnsureSuccessful() if(!viewEngineResult.Success)
.View; {
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) if (StatusCode != null)
{ {

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing;
using Microsoft.Framework.Logging;
using Moq; using Moq;
using Xunit; using Xunit;
@ -21,7 +22,7 @@ namespace Microsoft.AspNet.Mvc
"The view 'MyView' was not found. The following locations were searched:", "The view 'MyView' was not found. The following locations were searched:",
"Location1", "Location1",
"Location2."); "Location2.");
var actionContext = new ActionContext(new DefaultHttpContext(), var actionContext = new ActionContext(GetHttpContext(),
new RouteData(), new RouteData(),
new ActionDescriptor()); new ActionDescriptor());
var viewEngine = new Mock<IViewEngine>(); var viewEngine = new Mock<IViewEngine>();
@ -47,7 +48,7 @@ namespace Microsoft.AspNet.Mvc
{ {
// Arrange // Arrange
var viewName = "myview"; 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<IViewEngine>(); var viewEngine = new Mock<IViewEngine>();
var view = Mock.Of<IView>(); var view = Mock.Of<IView>();
@ -73,7 +74,7 @@ namespace Microsoft.AspNet.Mvc
{ {
// Arrange // Arrange
var viewName = "some-view-name"; var viewName = "some-view-name";
var context = new ActionContext(new DefaultHttpContext(), var context = new ActionContext(GetHttpContext(),
new RouteData(), new RouteData(),
new ActionDescriptor { Name = viewName }); new ActionDescriptor { Name = viewName });
var viewEngine = new Mock<ICompositeViewEngine>(); var viewEngine = new Mock<ICompositeViewEngine>();
@ -109,6 +110,8 @@ namespace Microsoft.AspNet.Mvc
var serviceProvider = new Mock<IServiceProvider>(); var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine))) serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
.Returns(viewEngine.Object); .Returns(viewEngine.Object);
serviceProvider.Setup(p => p.GetService(typeof(ILogger<PartialViewResult>)))
.Returns(new Mock<ILogger<PartialViewResult>>().Object);
context.HttpContext.RequestServices = serviceProvider.Object; context.HttpContext.RequestServices = serviceProvider.Object;
var viewResult = new PartialViewResult var viewResult = new PartialViewResult
@ -122,5 +125,17 @@ namespace Microsoft.AspNet.Mvc
// Assert // Assert
viewEngine.Verify(); viewEngine.Verify();
} }
private HttpContext GetHttpContext()
{
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(s => s.GetService(typeof(ILogger<PartialViewResult>)))
.Returns(new Mock<ILogger<PartialViewResult>>().Object);
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = serviceProvider.Object;
return httpContext;
}
} }
} }

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing;
using Microsoft.Framework.Logging;
using Moq; using Moq;
using Xunit; using Xunit;
@ -21,7 +22,8 @@ namespace Microsoft.AspNet.Mvc
"The view 'MyView' was not found. The following locations were searched:", "The view 'MyView' was not found. The following locations were searched:",
"Location1", "Location1",
"Location2."); "Location2.");
var actionContext = new ActionContext(new DefaultHttpContext(),
var actionContext = new ActionContext(GetHttpContext(),
new RouteData(), new RouteData(),
new ActionDescriptor()); new ActionDescriptor());
var viewEngine = new Mock<IViewEngine>(); var viewEngine = new Mock<IViewEngine>();
@ -47,7 +49,7 @@ namespace Microsoft.AspNet.Mvc
{ {
// Arrange // Arrange
var viewName = "myview"; 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<IViewEngine>(); var viewEngine = new Mock<IViewEngine>();
var view = Mock.Of<IView>(); var view = Mock.Of<IView>();
@ -73,7 +75,7 @@ namespace Microsoft.AspNet.Mvc
{ {
// Arrange // Arrange
var viewName = "some-view-name"; var viewName = "some-view-name";
var context = new ActionContext(new DefaultHttpContext(), var context = new ActionContext(GetHttpContext(),
new RouteData(), new RouteData(),
new ActionDescriptor { Name = viewName }); new ActionDescriptor { Name = viewName });
var viewEngine = new Mock<ICompositeViewEngine>(); var viewEngine = new Mock<ICompositeViewEngine>();
@ -109,6 +111,8 @@ namespace Microsoft.AspNet.Mvc
var serviceProvider = new Mock<IServiceProvider>(); var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine))) serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
.Returns(viewEngine.Object); .Returns(viewEngine.Object);
serviceProvider.Setup(p => p.GetService(typeof(ILogger<ViewResult>)))
.Returns(new Mock<ILogger<ViewResult>>().Object);
context.HttpContext.RequestServices = serviceProvider.Object; context.HttpContext.RequestServices = serviceProvider.Object;
var viewResult = new ViewResult var viewResult = new ViewResult
@ -122,5 +126,17 @@ namespace Microsoft.AspNet.Mvc
// Assert // Assert
viewEngine.Verify(); viewEngine.Verify();
} }
private HttpContext GetHttpContext()
{
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(s => s.GetService(typeof(ILogger<ViewResult>)))
.Returns(new Mock<ILogger<ViewResult>>().Object);
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = serviceProvider.Object;
return httpContext;
}
} }
} }