100 lines
4.4 KiB
C#
100 lines
4.4 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Serilog;
|
|
using Serilog.Core;
|
|
using Serilog.Events;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace Nirvana.Common
|
|
{
|
|
public class LogFactory
|
|
{
|
|
public static Logger _logger;
|
|
static LogFactory()
|
|
{
|
|
|
|
}
|
|
|
|
public static Logger GetLogger(IConfiguration configuration)
|
|
{
|
|
string outputTemplate = "{NewLine}【{Level:u3}】{Timestamp:yyyy-MM-dd HH:mm:ss.fff}" +
|
|
"{NewLine}#Msg#{Message:lj}" +
|
|
"{NewLine}#Pro #{Properties:j}" +
|
|
"{NewLine}#Exc#{Exception}" +
|
|
new string('-', 50);//输出模板
|
|
string filepath = Configs.GetString("logfile");
|
|
string date = DateTime.Now.ToString("yyyy-MM-dd");//按时间创建文件夹
|
|
_logger = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(configuration)
|
|
//2.1仅输出 LogEventLevel.Debug 类型
|
|
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Debug)//筛选过滤
|
|
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Debug}.log",
|
|
outputTemplate: outputTemplate,
|
|
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
|
encoding: Encoding.UTF8 // 文件字符编码
|
|
)
|
|
)
|
|
|
|
//2.2仅输出 LogEventLevel.Error 类型
|
|
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Error)//筛选过滤
|
|
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Error}.log",
|
|
outputTemplate: outputTemplate,
|
|
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
|
encoding: Encoding.UTF8 // 文件字符编码
|
|
)
|
|
)
|
|
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Information)//筛选过滤
|
|
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Information}.log",
|
|
outputTemplate: outputTemplate,
|
|
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
|
encoding: Encoding.UTF8 // 文件字符编码
|
|
)
|
|
)
|
|
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Fatal)//筛选过滤
|
|
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Fatal}.log",
|
|
outputTemplate: outputTemplate,
|
|
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
|
encoding: Encoding.UTF8 // 文件字符编码
|
|
)
|
|
)
|
|
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)//筛选过滤
|
|
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Warning}.log",
|
|
outputTemplate: outputTemplate,
|
|
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
|
encoding: Encoding.UTF8 // 文件字符编码
|
|
)
|
|
)
|
|
.CreateLogger();
|
|
return _logger;
|
|
}
|
|
|
|
public static void InsertErrorLog(string msg, int type = 1)
|
|
{
|
|
msg = $"{msg}";
|
|
switch (type)
|
|
{
|
|
case 1:
|
|
_logger.Error(msg);
|
|
break;
|
|
case 2:
|
|
_logger.Information(msg);
|
|
break;
|
|
case 3:
|
|
_logger.Warning(msg);
|
|
break;
|
|
case 4:
|
|
_logger.Debug(msg);
|
|
break;
|
|
case 5:
|
|
_logger.Fatal(msg);
|
|
break;
|
|
default:
|
|
_logger.Error(msg);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|