简体   繁体   English

共享启动配置 dotnet core 2 WebAPI

[英]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.在处理具有多个 API 的项目时,我们希望共享配置以确保每个 API 具有相同的记录器配置等,并且我们可以将所有 nuget 包保留在扩展类 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然后在启动ConfigureServices

// add config
services.AddExtensionsConfig();

and Startup Configure method和启动配置方法

// use config
app.UseExtensionsConfig();

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 asp.net-core 的模块化特性允许采用这种首选方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM