vs2019 microsoft 日志组件 配置

 2024-02-22 01:45:05      1688    0 

//1,添加 Serilog NuGet程序包

//2,在ConfigureServices 中添加以下代码
Log.Logger = new LoggerConfiguration()
//错误信息
.MinimumLevel.Error()
.MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error)
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Error)
//警告信息
.MinimumLevel.Warning()
.MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
//调试信息
.MinimumLevel.Debug()
.WriteTo.File(Path.Combine("Log", "log.txt"), rollingInterval: RollingInterval.Day)
.WriteTo.Console()
.CreateLogger();

//3,使用方法:
//在需要使用日志的类中添加以下代码:
private readonly ILogger _logger;
///
/// 构造函数
///

///
public DataManageController(ILogger logger)
{
_logger = logger;
}


//使用:
_logger.LogDebug($"return={checkmerch}|clientid={clientid}&sign={sign}");