转载请注明原文链接!
为什么要使用Serilog?
Serilog比NLog 功能更为强大,能支持结构化的消息,不像NLog扁平和繁琐,配置也更加方便。支持代码配置、json、xml。但我觉得用代码配置就挺好,nlog就是XML配置太麻烦。
1、nuget 安装
Serilog.AspNetCore 会自动安装依赖项
2、(可选,异步写入)nuget 安装 Serilog.Sinks.Async
3、注册服务配置写入文件
builder.Services.AddLogging(bulid =>
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
WriteTo.Async(config => config.File(
"d://.log",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1024 * 1024 * 10,//单位字节 不配置时默認1GB
retainedFileCountLimit: 10,//保留最近多少個文件 不配置时默認31個
rollOnFileSizeLimit: true,//超過文件大小時 自動創建新文件
shared: true)
))
.CreateLogger();
bulid.AddSerilog();
});
4、随便找个控制器在构造函数注入后即可使用
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable Get()
{
while (true)
{
_logger.LogInformation("ASDASDASDASDASDADSASDASDASDASDASDADSASDASDASDASDASDADSASDASDASDASDASDADS");
}
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
打开d盘找到即可看见日志文本
输出格式如下:
2022-05-28 00:12:35.949 +08:00 [INF] ASDASDASDASDASDADSASDASDASDASDASDADSASDASDASDASDASDADSASDASDAS
1、设置好4个路径,对应不同的日志类型
string infoPath = Directory.GetCurrentDirectory() + @"\logs\info\.log"; ;
string waringPath = Directory.GetCurrentDirectory() + @"\logs\waring\.log";
string errorPath = Directory.GetCurrentDirectory() + @"\logs\error\.log";
string fatalPath = Directory.GetCurrentDirectory() + @"\logs\fatal\.log";
2、(推荐,不设也行)设置好模板
string template = "{NewLine}时间:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}等级:{Level}{NewLine}来源:{SourceContext}{NewLine}具体消息:{Message}{NewLine}{Exception}";
3、具体配置
builder.Services.AddLogging(bulid =>
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.FromLogContext()
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Information).
WriteTo.Async(congfig => congfig.File(
infoPath,
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1024 * 1024 * 10,//默認1GB
retainedFileCountLimit: 10,//保留最近多少個文件 默認31個
rollOnFileSizeLimit: true,//超過文件大小時 自動創建新文件
shared: true,
outputTemplate: template)
))
//-----------------------------------------------------------------------------------
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Warning).
WriteTo.Async(congfig => congfig.File(
waringPath,
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1024 * 1024 * 10,
retainedFileCountLimit: 10,
rollOnFileSizeLimit: true,
shared: true,
outputTemplate: template)
))
//-----------------------------------------------------------------------------------
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Error).
WriteTo.Async(congfig => congfig.File(
errorPath,
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1024 * 1024 * 10,
retainedFileCountLimit: 10,
rollOnFileSizeLimit: true,
shared: true,
outputTemplate: template)
))
//-----------------------------------------------------------------------------------
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Fatal).
WriteTo.Async(congfig => congfig.File(
fatalPath,
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 1024 * 1024 * 10,
retainedFileCountLimit: 10,
rollOnFileSizeLimit: true,
shared: true,
outputTemplate: template)
))
//-----------------------------------------------------------------------------------
.CreateLogger();
bulid.AddSerilog();
代码有点多,建议自己做封装
只做了基本配置,不够用的话,具体更多配置或更高级用法自行查询官网。
https://github.com/serilog