Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared Startup Config dotnet core 2 WebAPI

working on a project with multiple API's, we'd like to share the config to make sure every API has the same logger config etc and we can keep all the nuget packages in Extensions class Project.

Using extensions to achieve this, is a good way to achieve a shared startup configuration? Is there a better way?

Extensions Class.

namespace Extensions
{
    public static class Extensions
    {
        public static IServiceCollection AddExtensionsConfig(this IServiceCollection services)
        {
            services.AddLoggerConfig();
            services.AddMvc();

            return services;
        }

        public static IApplicationBuilder UseExtensionsConfig(this IApplicationBuilder app)
        {
            //use logger
            app.UseLoggerConfig();

            // auth
            app.UseAuthentication();

            // set startup file to index.html for easy testing
            app.UseDefaultFiles();

            //enable CORS
            app.UseCorsConfig();

            //added support for static file
            app.UseStaticFiles();

            app.UseMvc();

            return app;
        }
    }
}

Logger Extension

namespace Extensions.Logger
{
    public static class LoggerServiceExtensions
    {
        public static IServiceCollection AddLoggerConfig(this IServiceCollection services)
        {
            //serilog
            services.AddLogging(loggingBuilder =>
             loggingBuilder.AddSerilog(dispose: true));

            var loggerServices = new ServiceCollection()
               .AddLogging(builder =>
               {
                   builder.AddSerilog();
               });

            return services;
        }

        public static IApplicationBuilder UseLoggerConfig(this IApplicationBuilder app)
        {
            //add serilog https://github.com/serilog/serilog-extensions-logging
            // https://github.com/serilog/serilog-extensions-logging/blob/dev/samples/Sample/Program.cs
            Log.Logger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger();

            var startTime = DateTimeOffset.UtcNow;

            Log.Logger.Information("Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);

            return app;
        }
    }
}

Then in Startup ConfigureServices

// add config
services.AddExtensionsConfig();

and Startup Configure method

// use config
app.UseExtensionsConfig();
like image 784
fuzzybear Avatar asked Aug 21 '26 20:08

fuzzybear


1 Answers

Using extensions to achieve this, is good way to achieve a shared startup configuration ? Is there a better way ?

This is currently the advised way to do it. You will also see this pattern in most examples provided and with 3rd part extensions. The modularised nature of asp.net-core allows for this preferred approach

like image 114
Nkosi Avatar answered Aug 24 '26 09:08

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!