public class Logger: interfaces.ILogger
{
public Logger()
{
}
private CSRedisClient csRedis = new CSRedisClient("10.185.3.130:6379,defaultDatabase=7");
const string log1Name = "ApiLog";
const string log2Name = "WebLog";
const string log3Name = "ErrorLog";
const string log4Name = "DebugLog";
const string log5Name = "WarningLog";
/// <summary>
/// 初始化日志
/// </summary>
public void InitLog()
{
RedisHelper.Initialization(csRedis);
string LogFilePath(string FileName) => $@"{AppContext.BaseDirectory}ALL_Logs\{FileName}\log.log";
string SerilogOutputTemplate = "{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel:{Level}{NewLine}Message:{Message}{NewLine}{Exception}" + new string('-', 100);
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Debug() // 所有Sink的最小记录级别
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(Matching.WithProperty<string>("position", p => p == log1Name)).WriteTo.Async(a => a.File(LogFilePath(log1Name), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(Matching.WithProperty<string>("position", p => p == log2Name)).WriteTo.Async(a => a.File(LogFilePath(log2Name), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(Matching.WithProperty<string>("position", p => p == log3Name)).WriteTo.Async(a => a.File(LogFilePath(log3Name), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(Matching.WithProperty<string>("position", p => p == log4Name)).WriteTo.Async(a => a.File(LogFilePath(log4Name), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(Matching.WithProperty<string>("position", p => p == log5Name)).WriteTo.Async(a => a.File(LogFilePath(log5Name), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)))
.CreateLogger();
}
/*****************************下面是不同日志级别*********************************************/
// FATAL(致命错误) > ERROR(一般错误) > Warning(警告) > Information(一般信息) > DEBUG(调试信息)>Verbose(详细模式,即全部)
/// <summary>
/// 普通日志
/// </summary>
/// <param name="msg"></param>
/// <param name="fileName"></param>
public void Info(string msg, string fileName = "")
{
if (fileName == "" || fileName == log1Name)
{
Log.Information($"{{position}}:{msg}", log1Name);
}
else if (fileName == log2Name)
{
Log.Information($"{{position}}:{msg}", log2Name);
}
else
{
//输入其他的话,还是存放到第一个文件夹
Log.Information($"{{position}}:{msg}", log1Name);
}
Task.Run(() => writeLogToRedis(msg, "infomation"));
}
/// <summary>
/// 调试日志
/// </summary>
/// <param name="msg"></param>
public void Debug(string msg)
{
Log.Debug($"{{position}}:{msg}", log4Name);
Task.Run(() => writeLogToRedis(msg, "debug"));
}
/// <summary>
/// 警告日志或一些关键日志
/// </summary>
/// <param name="msg"></param>
public void Warning(string msg)
{
Log.Warning($"{{position}}:{msg}", log5Name);
Task.Run(() => writeLogToRedis(msg, "warning"));
}
/// <summary>
/// Error方法统一存放到ErrorLog文件夹
/// </summary>
/// <param name="msg"></param>
public void Error(Exception ex)
{
Log.Error(ex, "{position}:" + ex.Message, log3Name);
Task.Run(() => writeLogToRedis(ex.Message, "error"));
}
public void Error(string msg)
{
Log.Error("{position}:" + msg, log3Name);
Task.Run(() => writeLogToRedis(msg, "error"));
}
/// <summary>
/// 日志写入到redis队列
/// </summary>
/// <param name="msg"></param>
/// <param name="logLevel"></param>
/// <returns></returns>
public async Task writeLogToRedis(string msg,string logLevel)
{
string ret = $"{logLevel}\"{msg}\"";
await RedisHelper.LPushAsync(ConfigurationHelper.GetSectionValue("redislogkey"), ret);
}
}
评论