Serilog在.net core中的使用
作者:互联网
1. Nu包安装Serilog.ASPNetCore和Serilog.Sinks.File
2. 在APPSettings中添加Serilog注入
"Serilog": { "WriteTo": [ { "Name": "File", "Args": { "path": "logs/log.txt", "rollingInterval": "Day", "outputTemplate": "Occurrence Time: {Timestamp:HH:mm:ss.fff} Level: {Level} Detailed Information: {Message}{NewLine}{Exception}" } } ] },
3. 在Program.cs中注册Serilog的引用
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Serilog; using System; namespace SuperNode.WebApi { public class Program { public static void Main(string[] args) { Log.Logger = new LoggerConfiguration(). MinimumLevel.Debug(). MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Information) .ReadFrom.Configuration(new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build()) .Enrich.FromLogContext(). /*WriteTo.File(Path.Combine("logs", @"log.txt"), (Serilog.Events.LogEventLevel)RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"). .WriteTo.MSSqlServer("Data Source=DESKTOP-4TU9A6M;Initial Catalog=CoreFrame;User ID=sa;Password=123456", "logs", autoCreateSqlTable: true, restrictedToMinimumLevel: LogEventLevel.Information)*/ CreateLogger(); try { Log.Information("SuperNode.DataPrivilegeCenter"); CreateHostBuilder(args).Build().Run(); } catch (Exception ex) { Log.Fatal(ex, "Host terminated unexpectedly"); } finally { Log.CloseAndFlush(); } } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseSerilog() .UseStartup<Startup>(); }); } }
4. 在Controller中使用
[Route("api/[controller]")] [ApiController] public class TestController : ControllerBase { private readonly ILogger<TestController> _logger; public TestController(ILogger<TestController> logger) { _logger = logger; } [HttpGet("getById")] public string Get() { return "Hello Web API World"; } [HttpPost] public string Add() { return ""; } [HttpPut] public string Put() { return ""; } [HttpDelete] public string Delete() { return ""; } }
5. 最终生成的Log文件
Occurrence Time: 09:46:02.288 Level: Information Detailed Information: SuperNode.DataPrivilegeCenter Occurrence Time: 09:46:03.296 Level: Information Detailed Information: Application started. Press Ctrl+C to shut down. Occurrence Time: 09:46:03.301 Level: Information Detailed Information: Hosting environment: "Development" Occurrence Time: 09:46:03.302 Level: Information Detailed Information: Content root path: "E:\SuperNode\SuperNode.UserPrivilegeCenter\WebApplication1" Occurrence Time: 09:46:03.328 Level: Information Detailed Information: Request starting HTTP/2 GET https://localhost:44307/swagger/index.html - - Occurrence Time: 09:46:03.760 Level: Information Detailed Information: Request finished HTTP/2 GET https://localhost:44307/swagger/index.html - - - 200 - text/html;charset=utf-8 434.8918ms Occurrence Time: 09:46:03.949 Level: Information Detailed Information: Request starting HTTP/2 GET https://localhost:44307/swagger/v1/swagger.json - - Occurrence Time: 09:46:04.161 Level: Information Detailed Information: Request finished HTTP/2 GET https://localhost:44307/swagger/v1/swagger.json - - - 200 - application/json;charset=utf-8 212.2678ms Occurrence Time: 09:46:10.729 Level: Information Detailed Information: Request starting HTTP/2 GET https://localhost:44307/UserInfot/Get - - Occurrence Time: 09:46:10.774 Level: Information Detailed Information: Executing endpoint '"WebApplication1.Controllers.UserInfotController.Get (SuperNode.UserPrivilegeCenter.API)"' Occurrence Time: 09:46:10.798 Level: Information Detailed Information: Route matched with "{action = \"Get\", controller = \"UserInfot\"}". Executing controller action with signature "System.Collections.Generic.List`1[SuperNode.UserPrivilegeCenter.Entities.UserInfo] Get()" on controller "WebApplication1.Controllers.UserInfotController" ("SuperNode.UserPrivilegeCenter.API"). Occurrence Time: 09:46:10.805 Level: Information Detailed Information: Executing "ObjectResult", writing value of type '"System.Collections.Generic.List`1[[SuperNode.UserPrivilegeCenter.Entities.UserInfo, SuperNode.UserPrivilegeCenter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"'. Occurrence Time: 09:46:10.828 Level: Information Detailed Information: Executed action "WebApplication1.Controllers.UserInfotController.Get (SuperNode.UserPrivilegeCenter.API)" in 25.3773ms Occurrence Time: 09:46:10.828 Level: Information Detailed Information: Executed endpoint '"WebApplication1.Controllers.UserInfotController.Get (SuperNode.UserPrivilegeCenter.API)"' Occurrence Time: 09:46:10.829 Level: Information Detailed Information: Request finished HTTP/2 GET https://localhost:44307/UserInfot/Get - - - 200 - application/json;+charset=utf-8 99.8546ms Occurrence Time: 09:46:12.740 Level: Information Detailed Information: Request starting HTTP/2 GET https://localhost:44307/UserInfot/Get - - Occurrence Time: 09:46:12.741 Level: Information Detailed Information: Executing endpoint '"WebApplication1.Controllers.UserInfotController.Get (SuperNode.UserPrivilegeCenter.API)"' Occurrence Time: 09:46:12.741 Level: Information Detailed Information: Route matched with "{action = \"Get\", controller = \"UserInfot\"}". Executing controller action with signature "System.Collections.Generic.List`1[SuperNode.UserPrivilegeCenter.Entities.UserInfo] Get()" on controller "WebApplication1.Controllers.UserInfotController" ("SuperNode.UserPrivilegeCenter.API"). Occurrence Time: 09:46:12.753 Level: Information Detailed Information: Executing "ObjectResult", writing value of type '"System.Collections.Generic.List`1[[SuperNode.UserPrivilegeCenter.Entities.UserInfo, SuperNode.UserPrivilegeCenter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"'. Occurrence Time: 09:46:12.754 Level: Information Detailed Information: Executed action "WebApplication1.Controllers.UserInfotController.Get (SuperNode.UserPrivilegeCenter.API)" in 12.8913ms Occurrence Time: 09:46:12.755 Level: Information Detailed Information: Executed endpoint '"WebApplication1.Controllers.UserInfotController.Get (SuperNode.UserPrivilegeCenter.API)"' Occurrence Time: 09:46:12.755 Level: Information Detailed Information: Request finished HTTP/2 GET https://localhost:44307/UserInfot/Get - - - 200 - application/json;+charset=utf-8 15.5016ms
标签:Information,core,Occurrence,Level,Serilog,09,Detailed,Time,net 来源: https://www.cnblogs.com/Doom555/p/14333513.html