Beispiel #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            ILoggerFactory loggerFactory,
            IEmailService mailService,
            IOptions <EmailSettings> emailSettings,
            IFileService fileService,
            UnitOfWork unitOfWork,
            IOptions <LogSettings> logSettings)
        {
            /*
             * public enum LogLevel
             * {
             *      Debug = 1,
             *      Verbose = 2,
             *      Information = 3,
             *      Warning = 4,
             *      Error = 5,
             *      Critical = 6,
             *      None = int.MaxValue
             * }
             */

            //database Logs
            LogSetting dataBaseLogSetting =
                logSettings.Value.Settings.FirstOrDefault(x => x.Type == LogType.Database);

            if (dataBaseLogSetting != null && dataBaseLogSetting.On)
            {
                loggerFactory.AddDatabaseLogger(
                    app.ApplicationServices.GetRequiredService <IServiceScopeFactory>()
                    .CreateScope()
                    .ServiceProvider,
                    dataBaseLogSetting.Level);
            }

            //File Logs
            LogSetting fileLogSetting =
                logSettings.Value.Settings.FirstOrDefault(x => x.Type == LogType.File);

            if (fileLogSetting != null && fileLogSetting.On)
            {
                loggerFactory.AddFileLogger(fileService, fileLogSetting.Level);
            }

            //Email Logs
            LogSetting emailLogSetting =
                logSettings.Value.Settings.FirstOrDefault(x => x.Type == LogType.Email);

            if (emailLogSetting != null && emailLogSetting.On)
            {
                loggerFactory.AddEmailLogger(mailService, emailSettings, emailLogSetting.Level);
            }

            //Development settings
            if (Environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();

                loggerFactory.AddDebug(LogLevel.Information);
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));

                app.UseExceptionHandler(errorApp =>
                {
                    errorApp.Run(async context =>
                    {
                        context.Response.StatusCode  = 500;                        // or another Status according to Exception Type
                        context.Response.ContentType = "text/html";

                        IExceptionHandlerFeature error =
                            context.Features.Get <IExceptionHandlerFeature>();
                        if (error != null)
                        {
                            await context.Response.WriteAsync(error.Error.ToHtml());
                        }
                    });
                });
            }
            else             //production
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseMiddleware <HttpContextLogging>();

            //Bring in Swagger definitions
            app.UseSwagger();

            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint(
                    "/swagger/v1/swagger.json",
                    "Api and Schema Definitions");
            });

            app.UseCors(corsPolicyBuilder =>
                        corsPolicyBuilder
                        .WithOrigins("http://localhost:57752")
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        );

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    "default",
                    "{controller=Home}/{action=Index}/{id?}");
            });
        }