1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Server.Kestrel.Core;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Logging.Console;
- var builder = WebApplication.CreateBuilder(args);
- //控制台日志格式
- builder.Services.AddLogging(opt =>
- {
- opt.AddSimpleConsole(p =>
- {
- p.TimestampFormat = "[dd HH:mm:ss] ";
- p.SingleLine = true;
- p.ColorBehavior = LoggerColorBehavior.Enabled;
- });
- });
- builder.Services.Configure<LoggerFilterOptions>(options =>
- {
- options.AddFilter((category, level) =>
- {
- if (level == LogLevel.Debug) return false;
- if (level == LogLevel.Trace) return false;
- if (category == "Microsoft.AspNetCore.Hosting.Diagnostics" && level == LogLevel.Information) return false;
- if (category == "Microsoft.AspNetCore.Routing.EndpointMiddleware" && level == LogLevel.Information) return false;
- if (category == "Microsoft.AspNetCore.Server.Kestrel" && level == LogLevel.Information) return false;
- if (category == "Microsoft.AspNetCore.Server.Kestrel.Http2" && level == LogLevel.Information) return false;
- if (category == "Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker" && level == LogLevel.Information) return false;
- if (category == "Microsoft.AspNetCore.Mvc.Infrastructure.ContentResultExecutor" && level == LogLevel.Information) return false;
- return true; // 其他日志保留
- });
- });
- //配置HTTP1、HTTP2端口
- builder.WebHost.UseKestrel((_, kso) =>
- {
- kso.ListenAnyIP(0, lo => lo.Protocols = HttpProtocols.Http2);
- kso.ListenAnyIP(0, lo => lo.Protocols = HttpProtocols.Http1);
- });
- builder.Services.AddControllers();
- var app = builder.Build();
- app.Use(async (context, next) =>
- {
- context.Request.EnableBuffering(); // this used to be EnableRewind
- await next(context);
- });
- app.MapControllers();
- app.MapGet("/", () =>"I am hub!");
- await app.RunAsync();
|