Ejemplo n.º 1
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
                              ILoggerFactory loggerFactory)
        {
            // Default registration.
            loggerFactory.AddProvider(new ColorConsoleLoggerProvider(
                                          new ColorConsoleLoggerConfiguration
            {
                LogLevel = LogLevel.Error,
                Color    = ConsoleColor.Red
            }));

            // Custom registration with default values.
            loggerFactory.AddColorConsoleLogger();

            // Custom registration with a new configuration instance.
            loggerFactory.AddColorConsoleLogger(new ColorConsoleLoggerConfiguration
            {
                LogLevel = LogLevel.Debug,
                Color    = ConsoleColor.Gray
            });

            // Custom registration with a configuration object.
            loggerFactory.AddColorConsoleLogger(c =>
            {
                c.LogLevel = LogLevel.Information;
                c.Color    = ConsoleColor.Blue;
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
Ejemplo n.º 2
0
        public static ILoggerFactory AddColorConsoleLogger(this ILoggerFactory loggerFactory, Action <ColorConsoleLoggerConfiguration> configure)
        {
            var config = new ColorConsoleLoggerConfiguration();

            configure(config);
            return(loggerFactory.AddColorConsoleLogger(config));
        }
Ejemplo n.º 3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, ILogger <Startup> logger, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(errorApp =>
                {
                    errorApp.Run(async context =>
                    {
                        context.Response.StatusCode  = 500;
                        context.Response.ContentType = "text/html";

                        await context.Response.WriteAsync("<html lang=\"en\"><body>\r\n");
                        await context.Response.WriteAsync("ERROR!<br><br>\r\n");

                        var exceptionHandlerPathFeature =
                            context.Features.Get <IExceptionHandlerPathFeature>();

                        if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
                        {
                            await context.Response.WriteAsync("File error thrown!<br><br>\r\n");
                        }

                        await context.Response.WriteAsync("<a href=\"/\">Home</a><br>\r\n");
                        await context.Response.WriteAsync("</body></html>\r\n");
                        await context.Response.WriteAsync(new string(' ', 512)); // IE padding
                    });
                });
                app.UseHsts();
            }
            int count = 0;

            app.Use(next => async context =>
            {
                using (new MyStopwatch(logger, $"Time {++count}"))
                {
                    await next(context);
                }
            });

            // Default registration.
            loggerFactory.AddProvider(new ColorConsoleLoggerProvider(
                                          new ColorConsoleLoggerConfiguration
            {
                LogLevel = LogLevel.Error,
                Color    = ConsoleColor.Red
            }));

            // Custom registration with default values.
            loggerFactory.AddColorConsoleLogger();

            // Custom registration with a new configuration instance.
            loggerFactory.AddColorConsoleLogger(new ColorConsoleLoggerConfiguration
            {
                LogLevel = LogLevel.Debug,
                Color    = ConsoleColor.Gray
            });

            // Custom registration with a configuration object.
            loggerFactory.AddColorConsoleLogger(c =>
            {
                c.LogLevel = LogLevel.Information;
                c.Color    = ConsoleColor.Blue;
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
            logger.LogInformation("test logger");
            app.Use(async(context, next) =>
            {
                Console.WriteLine($"This is a middlerware");
                await next.Invoke();
            });

            // branch
            // app.Map("/swagger", HandRequestDel);
            app.MapWhen(context => context.Request.Headers.ContainsKey("Query"), HandRequestDel);
            //app.Run(async context =>
            //{
            //    await context.Response.WriteAsync($"This is terminal middlerware");
            //});

            // cors
            app.UseCors();

            app.UseHttpsRedirection();
            app.UseDefaultFiles();

            app.UseSwagger();
            app.UseSwaggerUI(option =>
            {
                option.SwaggerEndpoint("/swagger/v1/swagger.json", "to do api");
                //option.InjectStylesheet("/swagger-ui/custom.css");
            });
            app.UseRouting();
            var options = new DefaultFilesOptions();

            options.DefaultFileNames.Clear();
            options.DefaultFileNames.Add("mydefault.html");
            app.UseDefaultFiles(options);

            // Set up custom content types - associating file extension to MIME type
            var provider = new FileExtensionContentTypeProvider();

            // Add new mappings
            provider.Mappings[".myapp"] = "application/x-msdownload";
            provider.Mappings[".htm3"]  = "text/html";
            provider.Mappings[".image"] = "image/png";
            // Replace an existing mapping
            provider.Mappings[".rtf"] = "application/x-msdownload";
            // Remove MP4 videos.
            provider.Mappings.Remove(".mp4");
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider      = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "MyStaticFiles")),
                RequestPath       = "/staticfiles",
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={6000}");
                },
                ContentTypeProvider = provider
            });

            app.UseMiddleware <ProductsLinkMiddleware>();

            app.Use(next => async context =>
            {
                using (new MyStopwatch(logger, $"Time {++count}"))
                {
                    await next(context);
                }
            });

            app.UseAuthentication();
            app.UseAuthorization();


            app.Use(next => async context =>
            {
                using (new MyStopwatch(logger, $"Time {++count}"))
                {
                    await next(context);
                }
            });

            // query endpoints
            app.Use(next => httpContext =>
            {
                var endPoints = httpContext.GetEndpoint();
                if (endPoints is null)
                {
                    return(Task.CompletedTask);
                }
                Console.WriteLine($"Endpoints:{endPoints.DisplayName}");
                if (endPoints is RouteEndpoint routeEndpoint)
                {
                    Console.WriteLine($"Endpoint has route pattern:" + routeEndpoint.RoutePattern.RawText);
                }
                foreach (var metadata in endPoints.Metadata)
                {
                    Console.WriteLine($"Endpoint has metadata: {metadata}");
                }
                return(next(httpContext));
            });



            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapGet("/hello/{name:alpha}", async context =>
                {
                    var name = context.Request.RouteValues["name"];
                    await context.Response.WriteAsync($"hello{name}");
                });

                // Configure the Health Check endpoint and require an authorized user.
                endpoints.MapHealthChecks("/healthz");
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/hello/{name:alpha}", async context =>
                {
                    var name = context.Request.RouteValues["name"];
                    await context.Response.WriteAsync($"hello{name}");
                });
            });
        }