Asp.net core 使用Serilog记录日志
Serilog 的使用记录
目录
1.安装依赖包
2.使用前配置
3.具体使用
1. 从 NuGet 安装 Serilog
核心的包是 Serilog.AspNetCore
建议安装 Serilog.AspNetCore,几乎包含了Serilog常用的所有包
如果想将日志写入数据库,需要安装
Serilog.Sinks.MSSqlServer
,或者写入日志服务器,需要安装
Serilog.Sinks.Seq
2. 配置
两种方式进行配置
1. 在Program文件中,Main方法和CreateHostBuilder方法中分别如下配置
public static void Main(string[] args) { string SerilogOutputTemplate = "{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel:{Level}{NewLine}Message:{Message}{NewLine}{Exception}" + new string('-', 50); Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() //日志记录的最小级别 .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Information) //如果类别是Microsoft开头的话,最小输出级别是information .WriteTo.Console() //输出到控制台 .WriteTo.File(@$"{Environment.CurrentDirectory}\Logs\{DateTime.Now.ToString("yyyy-MM-dd")}_log.txt", rollingInterval:RollingInterval.Day, outputTemplate: SerilogOutputTemplate) .CreateLogger(); CreateHostBuilder(args).Build().Run(); }
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .UseSerilog();
path:默认路径是程序的bin目录+path参数,当然也可以写绝对路径,只需要写入参数就可以了
rollingInterval:创建文件的类别,可以按分钟,小时,天,月。
outputTemplate:日志模板,可以自定义
2.在CreateHostBuilder 方法中
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); //或在此处添加serilog配置 webBuilder.UseSerilog((hostingContext, loggerConfiguration) => { loggerConfiguration .ReadFrom.Configuration(hostingContext.Configuration) .MinimumLevel.Debug() .Enrich.FromLogContext() .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.File(LogFilePath("Debug"), rollingInterval: RollingInterval.Hour, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File(LogFilePath("Information"), rollingInterval: RollingInterval.Hour, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File(LogFilePath("Warning"), rollingInterval: RollingInterval.Hour, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File(LogFilePath("Error"), rollingInterval: RollingInterval.Hour, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File(LogFilePath("Fatal"), rollingInterval: RollingInterval.Hour, outputTemplate: SerilogOutputTemplate)) .WriteTo.Console(); }); });
WriteTo.Logger 可以将不同级别的日志文件输出到不同的文件夹内
3. 使用
using Microsoft.Extensions.Logging;
using Serilog;
[Route("api/[controller]")] [ApiController] public class UserController : ControllerBase { private readonly ILogger<UserController> _logger = null; public UserController(IUserService userService,ILogger<UserController> logger) { _logger = logger; } [HttpGet] [Route("GetLog")] public ActionResult<IEnumerable<string>> Get() { //以下使用都会将日志由Serilog 输出到文件中 Log.Error("这是错误日志"); Log.Debug("这是Debug日志"); Log.Information("这是Information日志"); _logger.LogDebug("Test log"); return new string[] { "value1", "value2" }; } }
我要一步一步往上爬
在最高点乘着叶片往前飞
任风吹干 流过的泪和汗
我要一步一步往上爬
等待阳光静静看着它的脸
小小的天有大大的梦想
我有属于我的天
任风吹干流过的泪和汗
总有一天我要属于我的天