Skip to content

Adding warning log when no actions detected #39618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNetCore.Mvc.Infrastructure;

internal class DefaultActionDescriptorCollectionProvider : ActionDescriptorCollectionProvider
internal partial class DefaultActionDescriptorCollectionProvider : ActionDescriptorCollectionProvider
{
private readonly IActionDescriptorProvider[] _actionDescriptorProviders;
private readonly IActionDescriptorChangeProvider[] _actionDescriptorChangeProviders;
private readonly ILogger _logger;

// The lock is used to protect WRITES to the following (do not need to protect reads once initialized).
private readonly object _lock;
Expand All @@ -25,7 +27,8 @@ internal class DefaultActionDescriptorCollectionProvider : ActionDescriptorColle

public DefaultActionDescriptorCollectionProvider(
IEnumerable<IActionDescriptorProvider> actionDescriptorProviders,
IEnumerable<IActionDescriptorChangeProvider> actionDescriptorChangeProviders)
IEnumerable<IActionDescriptorChangeProvider> actionDescriptorChangeProviders,
ILogger<DefaultActionDescriptorCollectionProvider> logger)
{
_actionDescriptorProviders = actionDescriptorProviders
.OrderBy(p => p.Order)
Expand All @@ -35,6 +38,8 @@ public DefaultActionDescriptorCollectionProvider(

_lock = new object();

_logger = logger;

// IMPORTANT: this needs to be the last thing we do in the constructor. Change notifications can happen immediately!
ChangeToken.OnChange(
GetCompositeChangeToken,
Expand Down Expand Up @@ -124,6 +129,13 @@ private void UpdateCollection()
_actionDescriptorProviders[i].OnProvidersExecuted(context);
}

if (context.Results.Count == 0)
{
// Emit a log message if after all providers still no action
// descriptors detected in the context.
Log.NoActionDescriptors(_logger);
}

// The sequence for an update is important because we don't want anyone to obtain
// the new change token but the old action descriptor collection.
// 1. Obtain the old cancellation token source (don't trigger it yet)
Expand Down Expand Up @@ -156,4 +168,14 @@ private void UpdateCollection()
oldCancellationTokenSource?.Cancel();
}
}

public static partial class Log
{
[LoggerMessage(
EventId = 1,
EventName = "NoActionDescriptors",
Level = LogLevel.Information,
Message = "No action descriptors found. This may indicate an incorrectly configured application or missing application parts. To learn more, visit https://aka.ms/aspnet/mvc/app-parts")]
public static partial void NoActionDescriptors(ILogger logger);
}
}
2 changes: 1 addition & 1 deletion src/Mvc/Mvc.Core/src/MvcCoreLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace Microsoft.AspNetCore.Mvc;

internal static class MvcCoreLoggerExtensions
internal static partial class MvcCoreLoggerExtensions
{
public const string ActionFilter = "Action Filter";
private static readonly string[] _noFilters = new[] { "None" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;

namespace Microsoft.AspNetCore.Mvc.ActionConstraints;

Expand Down Expand Up @@ -157,7 +158,8 @@ private static ActionConstraintCache CreateCache(params IActionConstraintProvide
{
var descriptorProvider = new DefaultActionDescriptorCollectionProvider(
Enumerable.Empty<IActionDescriptorProvider>(),
Enumerable.Empty<IActionDescriptorChangeProvider>());
Enumerable.Empty<IActionDescriptorChangeProvider>(),
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);
return new ActionConstraintCache(descriptorProvider, providers);
}
}
6 changes: 4 additions & 2 deletions src/Mvc/Mvc.Core/test/Infrastructure/ActionSelectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,8 @@ private ControllerActionDescriptor InvokeActionSelector(RouteContext context)
var actionDescriptorProvider = GetActionDescriptorProvider();
var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
new[] { actionDescriptorProvider },
Enumerable.Empty<IActionDescriptorChangeProvider>());
Enumerable.Empty<IActionDescriptorChangeProvider>(),
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);

var actionConstraintProviders = new[]
{
Expand Down Expand Up @@ -1134,7 +1135,8 @@ private static ActionConstraintCache GetActionConstraintCache(IActionConstraintP
{
var descriptorProvider = new DefaultActionDescriptorCollectionProvider(
Enumerable.Empty<IActionDescriptorProvider>(),
Enumerable.Empty<IActionDescriptorChangeProvider>());
Enumerable.Empty<IActionDescriptorChangeProvider>(),
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);
return new ActionConstraintCache(descriptorProvider, actionConstraintProviders.AsEnumerable() ?? new List<IActionConstraintProvider>());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Primitives;
using Moq;

Expand All @@ -22,7 +23,8 @@ public void ActionDescriptors_ReadsDescriptorsFromActionDescriptorProviders()

var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
new[] { actionDescriptorProvider1, actionDescriptorProvider2 },
Enumerable.Empty<IActionDescriptorChangeProvider>());
Enumerable.Empty<IActionDescriptorChangeProvider>(),
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);

// Act
var collection = actionDescriptorCollectionProvider.ActionDescriptors;
Expand All @@ -44,7 +46,8 @@ public void ActionDescriptors_CachesValuesByDefault()

var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
new[] { actionDescriptorProvider },
Enumerable.Empty<IActionDescriptorChangeProvider>());
Enumerable.Empty<IActionDescriptorChangeProvider>(),
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);

// Act - 1
var collection1 = actionDescriptorCollectionProvider.ActionDescriptors;
Expand Down Expand Up @@ -93,7 +96,8 @@ public void ActionDescriptors_UpdatesAndResubscribes_WhenChangeTokenTriggers()
var changeProvider = new TestChangeProvider();
var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
new[] { actionDescriptorProvider.Object },
new[] { changeProvider });
new[] { changeProvider },
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);

// Act - 1
var changeToken1 = actionDescriptorCollectionProvider.GetChangeToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Primitives;
using Moq;

Expand Down Expand Up @@ -129,7 +130,8 @@ private protected ActionEndpointDataSourceBase CreateDataSource(IActionDescripto
{
actions = new DefaultActionDescriptorCollectionProvider(
Array.Empty<IActionDescriptorProvider>(),
Array.Empty<IActionDescriptorChangeProvider>());
Array.Empty<IActionDescriptorChangeProvider>(),
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);
}

var services = new ServiceCollection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;

namespace Microsoft.AspNetCore.Mvc.Routing;
Expand Down Expand Up @@ -285,7 +286,8 @@ private static IActionDescriptorCollectionProvider CreateActionDescriptorCollect

var descriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
new[] { actionProvider.Object },
Enumerable.Empty<IActionDescriptorChangeProvider>());
Enumerable.Empty<IActionDescriptorChangeProvider>(),
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);
return descriptorCollectionProvider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Matching;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;

namespace Microsoft.AspNetCore.Mvc.Routing;
Expand Down Expand Up @@ -449,7 +450,8 @@ private ActionConstraintMatcherPolicy CreateSelector(ActionDescriptor[] actions)

var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
new IActionDescriptorProvider[] { actionDescriptorProvider.Object, },
Enumerable.Empty<IActionDescriptorChangeProvider>());
Enumerable.Empty<IActionDescriptorChangeProvider>(),
NullLogger<DefaultActionDescriptorCollectionProvider>.Instance);

var cache = new ActionConstraintCache(actionDescriptorCollectionProvider, new[]
{
Expand Down