C# web api 日志

一、依赖

1、Serilog.AspNetCore

2、Serilog.Sinks.File

二、配置(Program.cs)

#region 日志
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information() // 最低级别
    .WriteTo.Console()          // 输出到console
                                // 输出到本地文档
    .WriteTo.File(
        path: $"logs/{Assembly.GetExecutingAssembly().GetName().Name}-.txt", // logs文件
        rollingInterval: RollingInterval.Day, // app-log-20231025.txt
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}" // 格式
    )
    .CreateLogger();
builder.Host.UseSerilog();
#endregion

builder.Services.AddControllers();

 

posted @ 2026-07-13 17:12  市丸银  阅读(5)  评论(0)    收藏  举报