安装 Serilog
public static class LoggerExtension
{
public static ILogger<T> CreateLogger<T>()
{
var logPath = "log.txt";
const string outputTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";
Logger logger = new LoggerConfiguration()
.WriteTo.File(logPath, outputTemplate: outputTemplate, rollingInterval: RollingInterval.Day, shared: true, rollOnFileSizeLimit: true)
.CreateLogger();
SerilogLoggerProvider serilogLoggerProvider = new SerilogLoggerProvider(logger);
ILoggerFactory loggerFactory = App.Services.GetRequiredService<ILoggerFactory>();
loggerFactory.AddProvider(serilogLoggerProvider);
return loggerFactory.CreateLogger<T>();
}
}
标签:Serilog,SerilogLoggerProvider,CreateLogger,Host,ILogger,loggerFactory,outputTemp
From: https://www.cnblogs.com/fires/p/18081787