日志组件
# 敢为公共库
基于Serilog,对日志模块进行封装 提供单例模式及独立日志模式,默认日志保存目录为log文件夹
# 依赖库
序号 | 名称 | 版本 | 说明 |
---|---|---|---|
1 | Serilog | 2.11.0 | 日志核心库 |
2 | Serilog.Settings.Configuration | 3.1.0 | json配置扩展 |
3 | Microsoft.Extensions.Configuration.Json | 6.0.0 | json配置读取 |
4 | Serilog.Sinks.File | 5.0.0 | 文件记录扩展 |
5 | Serilog.Expressions | 3.4.0 | 表达式扩展 |
6 | Serilog.Enrichers.Thread | 3.1.0 | 线程扩展 |
7 | IoTCenter.Extensions.RollingFile | 1.0.3 | 基于Serilog.Sinks.File对滚动文件存储进行扩展 |
- 配置文件 默认配置文件为 bin/logsetting.json 提供全局配置及独立模块配置,可根据需要进行修改
# 安装教程
通过Nuget(https://nuget.ganweicloud.com)安装
Install-Package IoTCenter.Extensions.Logging
# 使用说明
# 1. 通过工厂调用 (单例)
使用默认配置将输出到log/log.txt中
using IoTCenter.Extensions.Logging;
LoggerFactory.Default.Debug("这是Debug信息");
LoggerFactory.Default.Info("这是Info信息");
LoggerFactory.Default.Warn("这是Warn信息");
LoggerFactory.Default.Error("这是Error信息");
# 2. 通过依赖注入
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//默认配置
services.AddDefaultSeriLogging();
// 自定义配置
services.AddSeriLogging((provider, config) =>
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("logsetting.json");
var configuration = builder.Build();
config.ReadFrom.Configuration(configuration);
});
}
//使用方法
public class DomeService
{
public DomeService(ILogger logger)
{
logger.Debug("这是Debug信息");
}
}
# 3. 独立日志输出
用于模块日志独立输出,与默认日志分离 根据配置将日志输出到log/{Module}/.txt中
// 以设备名称为例
var newLogger = LoggerFactory.CreateLogger("ModuleName");
newLogger.Debug("这是Debug信息");
newLogger.Info("这是Info信息");
newLogger.Warn("这是Warn信息");
newLogger.Error("这是Error信息");
# 配置
默认配置从程序的运行目录读取logsetting.json
使用默认配置将输出到log/log.txt中
单文件最大大小:10M
最大重复文件 10 个
若使用独立日志模式创建实例,则将日志输出到log/{Module}/.txt中 Module为创建时传入名称,则根据实际日志等级独立输出
# logsetting.json 默认配置
logsetting.json
{
"Serilog": {
"Using": [
"IoTCenter.Extensions.RollingFile",
"Serilog.Expressions",
"Serilog.Enrichers.Thread"
],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@p['Context'] is null"
}
}
],
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"path": "..\\log\\log.txt",
"fileSizeLimitBytes": "10485760",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"outputTemplate": "[{Timestamp:HH:mm:ss.fff zzz} {Level:u3} ({ThreadId})]{NewLine}{Message:lj}{NewLine}",
"retainedFileCountLimit": 30
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@p['Context'] is not null and @l ='Debug' "
}
}
],
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"path": "..\\log\\{Context}\\Debug.txt",
"fileSizeLimitBytes": "10485760",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"outputTemplate": "[{Timestamp:HH:mm:ss.fff zzz} {Level:u3} ({ThreadId})] {Message:lj}{NewLine}{Exception}",
"retainedFileCountLimit": 30
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@p['Context'] is not null and @l ='Information' "
}
}
],
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"path": "..\\log\\{Context}\\Info.txt",
"fileSizeLimitBytes": "10485760",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"outputTemplate": "[{Timestamp:HH:mm:ss.fff zzz} {Level:u3} ({ThreadId})] {Message:lj}{NewLine}{Exception}",
"retainedFileCountLimit": 30
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@p['Context'] is not null and ( @l ='Warning' or @l ='Error' or @l ='Fatal') "
}
}
],
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"path": "..\\log\\{Context}\\Error.txt",
"fileSizeLimitBytes": "10485760",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"outputTemplate": "[{Timestamp:HH:mm:ss.fff zzz} {Level:u3} <{ThreadId}>] {Message:lj}{NewLine}{Exception}",
"retainedFileCountLimit": 30
}
}
]
}
}
}
],
"Enrich": [
"FromLogContext",
"WithThreadId"
]
}
}
上次更新: 6/19/2023, 9:27:24 AM