当前位置: 首页 > news >正文

Serilog日志框架

文章目录

    • 一. Serilog介绍
      • 1.1 安装Serilog
      • 1.2 Serilog日志级别
    • 二. Serilog.App项目应用
      • 2.1 Serilog一般应用
      • 2.2 兼容系统日志
    • 三. Serilog.Web应用
      • 3.1 Minimal示例
      • 3.2 WebApi示例
    • 参考链接

一. Serilog介绍

Serilog 是 .NET应用程序的诊断日志记录库。它易于设置,具有干净的 API,并且可以在所有最新的 .NET 平台上运行。虽然它即使在最简单的应用程序中也很有用,但 Serilog 对结构化日志记录的支持在检测复杂、分布式和异步应用程序和系统时大放异彩。

1.1 安装Serilog

dotnet add package Serilog
dotnet add package Serilog.Extensions.Logging
dotnet add package Serilog.Sinks.File
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.Async

1.2 Serilog日志级别

public enum LogEventLevel {Verbose,       // 相当于TraceDebug,     	 // 调试模式Information,   // 提示信息Warning,       // 警告信息Error,         // 错误信息Fatal          // 验证错误
}

二. Serilog.App项目应用

2.1 Serilog一般应用

  • 可使用Serilog.Log.ForContext<T>()构建类型化ILogger对象。
  • 可使用Serilog.Log类的静态ILogger成员直接打印日志内容。
using Demo.Service;
using Serilog;// Log.Logger是Serilog的一个静态成员,作为全局的ILogger对象
Log.Logger = new LoggerConfiguration().WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}]  {SourceContext} {Message} {Exception}{NewLine}").MinimumLevel.Verbose().WriteTo.File("Logs/Log.txt").CreateLogger();// Main中打印日志
Log.Information("应用程序已启动");// 其他业务代码...
var foo = new FooService();
foo.PrintWithNewLogger();
foo.PrintWithStaticLogger();// 关闭日志记录器并刷新缓冲区
Log.CloseAndFlush();
Console.ReadKey();
using Serilog;namespace Demo.Service;/// <summary>
/// 示例逻辑服务
/// </summary>
public class FooService {private readonly ILogger _logger;public FooService () {// 底层调用了CreateLogger创建了新的ILogger对象_logger = Log.ForContext<FooService>();}/// <summary>/// 构造ILogger/// </summary>public void PrintWithNewLogger () {_logger.Information("构建的ILoggre对象");_logger.Error(new Exception("发生异常"), "构建的ILoggre对象");}/// <summary>/// 静态的ILogger成员/// </summary>public void PrintWithStaticLogger () {Serilog.Log.Logger.Error("使用Serilog.Log.Logger打印日志");// 可简化为Serilog.Log.XXX(内部依然使用了Serilog.Log.Logger.XXX)Serilog.Log.Debug("使用Serilog.Log静态方法打印日志");Serilog.Log.Information("使用Serilog.Log静态方法打印日志");Serilog.Log.Warning("使用Serilog.Log静态方法打印日志");Serilog.Log.Error("使用Serilog.Log静态方法打印日志");Serilog.Log.Fatal("使用Serilog.Log静态方法打印日志");Serilog.Log.Verbose("等价于Trace级别的日志");}
}

2.2 兼容系统日志

using Microsoft.Extensions.Logging;
using Serilog;// 配置Serilog日志
var tpl = "时间: {Timestamp:yyyy-MM-dd HH:mm:ss}{NewLine}来源: {SourceContext}{NewLine}内容: [{Level:u3}] {Message}{NewLine}{Exception}{NewLine}";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console(outputTemplate: tpl)
.WriteTo.File("Logs/Log.txt", rollingInterval: RollingInterval.Day, outputTemplate: tpl)
.CreateLogger();// ⚠ 加入到Microsoft.Extensions.Logging 日志工厂
var fac = LoggerFactory.Create(config => { config.AddSerilog(); });// 在具体的类型化对象中使用Logger
var logger = fac.CreateLogger<Program>();
logger.LogInformation("这是一条提示信息");
logger.LogWarning("这是一条警告信息");
logger.LogError("这是一条错误信息");
logger.LogCritical("这是一条严重错误");Log.CloseAndFlush();
Console.ReadKey();

三. Serilog.Web应用

3.1 Minimal示例

  • 可将Serilog注入到IServiceCollection中,即依赖注入。
  • Serilog可打印系统捕获的异常信息,而无需显式指定异常输出格式。
using Serilog;
using Serilog.Events;var builder = WebApplication.CreateBuilder(args);// 配置Serilog日志
var tpl = "时间: {Timestamp:yyyy-MM-dd HH:mm:ss}{NewLine}来源: {SourceContext}{NewLine}内容: [{Level:u3}] {Message}{NewLine}{Exception}{NewLine}";
Log.Logger = new LoggerConfiguration().MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .WriteTo.Console(outputTemplate: tpl).WriteTo.File("Logs/Log.txt", rollingInterval: RollingInterval.Day, outputTemplate: tpl).CreateLogger();//将Serilog注入到应用
builder.Services.AddLogging(opt => {opt.ClearProviders();opt.AddSerilog(dispose: true);
});var app = builder.Build();
app.MapGet("/", () => {// 动态对象var logger = Log.ForContext<Program>();logger.Error("这是一条Serilog.ILogger信息");logger.Fatal("这是一条Serilog.ILogger信息");// 静态方法Log.Information("这是一条Serilog.ILogger信息");Log.Warning("这是一条Serilog.ILogger信息");// 从注入容器读取app.Logger.LogInformation("这是一条系统ILogger信息");app.Logger.LogError("这是一条系统ILogger信息");return "Hello world";
});app.MapGet("/exp", () => {throw new Exception("测试异常日志");
});app.Run();
Log.CloseAndFlush();	// 关闭日志记录器并刷新缓冲区

3.2 WebApi示例

Extends

using Serilog.Events;
using Serilog;namespace Awesome;public static class Extends {const string infoPath = "Logs/info.log";const string warnPath = "Logs/warn.log";const string errorPath = "Logs/error.log";const string fatalPath = "Logs/fatal.log";const string template = "时间: {Timestamp:yyyy-MM-dd HH:mm:ss}{NewLine}来源: {SourceContext}{NewLine}内容: [{Level:u3}] {Message}{NewLine}{Exception}{NewLine}";// 可以将日志输出到控制台、文件、数据库、ES等public static void AddSerilog (this IServiceCollection c) {Log.Logger = new LoggerConfiguration().MinimumLevel.Information().MinimumLevel.Override("Microsoft", LogEventLevel.Warning) // 排除Dotnet自带的日志.Enrich.FromLogContext().WriteTo.Console(outputTemplate: template).WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Information).WriteTo.Async(congfig => congfig.File(infoPath,rollingInterval: RollingInterval.Day,fileSizeLimitBytes: 1024 * 1024 * 10,    //默认1GBretainedFileCountLimit: 10,                   //保留最近多少个文件,默认31个rollOnFileSizeLimit: true,                       //超过文件大小时,自动创建新文件  shared: true,outputTemplate: template))).WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Warning).WriteTo.Async(congfig => congfig.File(warnPath,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();// 注入到容器c.AddLogging(opt => {opt.ClearProviders();opt.AddSerilog(dispose: true);});}
}

Controllers

using Microsoft.AspNetCore.Mvc;namespace Awesome.Controllers;[ApiController]
[Route("[controller]/[action]")]
public class HomeController : ControllerBase {private readonly ILogger<HomeController> _logger;public HomeController (ILogger<HomeController> logger) {_logger = logger;}[HttpGet]public IActionResult Index () {_logger.LogDebug("这是一条调试消息");_logger.LogInformation("这是一条提示消息");_logger.LogWarning("这是一条警告消息");_logger.LogError("这是一条错误消息");_logger.LogCritical("这是一条严重错误");return Ok("Hello World");}
}

Program

using Awesome;
using Serilog;var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();// 添加Serilog日志
builder.Services.AddSerilog();var app = builder.Build();
app.MapControllers();
app.Run();// 关闭日志流
Log.CloseAndFlush();

参考链接

https://serilog.net/

相关文章:

  • Java- maven下载jar包,提示找不到,Could not find artifact
  • 机器学习-关联规则算法Apriori及编码实现
  • 【讲解下Gitea】
  • PyTorch 教程-快速上手指南
  • 关于 UnityEditorWindow
  • Java中常见的锁策略
  • 【Linux】详解文件系统以及周边知识
  • 10.windows ubuntu 组装软件:spades,megahit
  • 鸿蒙应用开发-录音保存并播放音频
  • Linux文件(系统)IO(含动静态库的链接操作)
  • 最新2024年增强现实(AR)营销指南(完整版)
  • 全国青少年软件编程(Python)等级考试一级考试真题2023年9月——持续更新.....
  • HTML块级元素和内联元素(头部和布局)
  • centos 7 安装磐维(PanWeiDB)数据库(单机)
  • pandas在循环中多次写入数据到一个excel防止锁定的方法
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • canvas 五子棋游戏
  • crontab执行失败的多种原因
  • ES2017异步函数现已正式可用
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • IDEA常用插件整理
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • SpringCloud集成分布式事务LCN (一)
  • 工作中总结前端开发流程--vue项目
  • 力扣(LeetCode)21
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 系统认识JavaScript正则表达式
  • FaaS 的简单实践
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • "无招胜有招"nbsp;史上最全的互…
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (Git) gitignore基础使用
  • (Note)C++中的继承方式
  • (分布式缓存)Redis分片集群
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (转)Windows2003安全设置/维护
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • (转)为C# Windows服务添加安装程序
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • (转载)PyTorch代码规范最佳实践和样式指南
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • .form文件_SSM框架文件上传篇
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .Net 路由处理厉害了
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .NET/C# 项目如何优雅地设置条件编译符号?