Skip to content
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

GrandOutput is not configured yet in ConfigureServices() #4

Open
bcrosnier opened this issue Nov 7, 2017 · 0 comments
Open

GrandOutput is not configured yet in ConfigureServices() #4

bcrosnier opened this issue Nov 7, 2017 · 0 comments

Comments

@bcrosnier
Copy link
Member

We've been using the ActivityMonitor in WebHostBuilder.ConfigureServices(), but any monitor we use there does not output to file.
Upon further inspection, this is basically caused by CK-AspNet's StartupFilter; StartupFilters are executed after the whole ConfigureServices() pipeline (which makes sense, since they're registered in the ConfigureServices() pipeline).

Example code:

using CK.Core;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace WebApplication3
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseMonitoring()
                .ConfigureServices(services =>
                {
                    // GrandOutput doesn't have any handler yet
                    ActivityMonitor m = new ActivityMonitor();
                    m.Error("Error in ConfigureServices()");

                    // Register startup filter
                    services.AddSingleton<IStartupFilter, StartupFilterTest>();
                })
                .Configure((app) =>
                {
                    // Register ApplicationStarted
                    app.ApplicationServices.GetRequiredService<IApplicationLifetime>().ApplicationStarted.Register(OnApplicationStarted);
                })
                .Build();

        public class StartupFilterTest : IStartupFilter
        {
            public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => (app) =>
            {
                // GrandOutput is configured
                ActivityMonitor m = new ActivityMonitor();
                m.Error("Error in StartupFilter");

                next?.Invoke(app);
            };
        }

        private static void OnApplicationStarted()
        {
            // GrandOutput is configured
            ActivityMonitor m = new ActivityMonitor();
            m.Error("Error in ApplicationStarted");
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant