[記錄].net core 3.0 中配置Nlog和Swagger

@[TOC].net core3.0 中配置Nlog和Swagger

NLog的配置

需要在program.cs文件中做出如下配置

   public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                }).ConfigureLogging(logging =>
                {
                	//這裏配置Nlog
                    logging.ClearProviders();
                    logging.ConfigureNLog("nlog.config");// SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                })
              .UseNLog();
    }

以上是基於已經有.netcore和Nlog的使用經驗。

Swagger

配置StartUp.cs文件

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddMvc(config =>
            {
                config.Filters.Add<LogFilter>();
            });
            services.AddMvcCore();

            //註冊Swagger生成器,定義一個和多個Swagger 文檔
            services.AddSwaggerGen(option =>
            {
                option.SwaggerDoc(this.GetType().Namespace, new OpenApiInfo
                {
                    Version = GetType().Assembly.GetName().Version.ToString(),
                    Title = this.GetType().Namespace,
                    Description = "API for " + this.GetType().Namespace,
                    Contact = new OpenApiContact() { Name = "Lampard", Email = "[email protected]" }
                });

  #region 這裏可以選擇配置,配置以後支持驗證信息,對需要走驗證的接口測試比較方便
                option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                {
                    Description = "請在下方輸入驗證信息,格式: Bearer token,注意需要有空格,將token換成你的token值",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                });
                option.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    { new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference()
                        {
                            Id = "Bearer",
                            Type = ReferenceType.SecurityScheme
                        }
                    }, Array.Empty<string>() }
                });
#endregion
                // include document file
                option.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, this.GetType().Namespace + ".xml"), true);
            });
        }
        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param> 
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseHttpsRedirection();
            app.UseDefaultFiles();
            app.UseStaticFiles();
            
            //Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();
            //Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint
            app.UseSwaggerUI(option =>
            {
                option.SwaggerEndpoint("/swagger/" + this.GetType().Namespace + "/swagger.json", "Version " + GetType().Assembly.GetName().Version.ToString());

            });
        }
發佈了53 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章