NET 8使用Serilog + Seq
1.nuget安装:

2.Program.cs
SatartupHelper.CreateHostBuilder(args).Build().Run();
3.SatartupHelper.cs
public static class SatartupHelper { public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) // UseSerilog((hostingContext, loggerConfiguration) => //{ // loggerConfiguration // .MinimumLevel.Information() // .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) // .Enrich.FromLogContext() // .Enrich.WithMachineName() // .Enrich.WithThreadId() // .WriteTo.Async(a => // { // a.Console(); // a.File( // path: "Logs/log-.txt", // rollingInterval: RollingInterval.Day, // outputTemplate: "{时间:yyyy-MM-dd HH:mm:ss} [{等级:u3}] {内容:lj} (Thread:{ThreadId}){NewLine}{Exception}"); // }, bufferSize: 10000); //}) .UseSerilog((hostingContext, loggerConfiguration) => { string logPath = Path.Combine(AppContext.BaseDirectory, "Logs", "log-.txt"); loggerConfiguration .MinimumLevel.Information() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) // 单独放行 .Enrich.FromLogContext() .Enrich.WithMachineName() .Enrich.WithThreadId() .WriteTo.Seq( serverUrl: "http://localhost:5341", apiKey: ""/* builder.Configuration["Seq:ApiKey"]*/, controlLevelSwitch: null, messageHandler: null //compact: true ) .WriteTo.Async(a => { a.Console(); a.File( path: logPath, // 改为 .txt rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj} (ThreadId:{ThreadId}){NewLine}{Exception}", encoding: Encoding.UTF8); // 确保支持中文); }, bufferSize: 10000) ; }) //.ConfigureLogging(logging => //{ // logging.ClearProviders(); // 清除所有默认的日志提供者 // logging.AddConsole(); // 添加控制台日志提供者 // logging.AddDebug(); // 添加调试日志提供者(仅限开发环境) // logging.AddEventLog(); // 添加事件日志提供者(Windows 环境) // // 可以根据需要添加更多提供者,例如:logging.AddFile("路径"); //}) .ConfigureWebHostDefaults(webBuilder => { // 配置Kestrel 服务器选项 webBuilder.ConfigureKestrel(serverOptions => { serverOptions.Limits.MaxRequestBodySize = 10L * 1024 * 1024 * 1024; // 10GB }); webBuilder.UseStartup<Startup>(); }) .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); }); }
4.安装 Seq 服务器:
5.运行Core程序访问
http://localhost:5341/
6.效果:


浙公网安备 33010602011771号