103 lines
2.9 KiB
C#
103 lines
2.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace SA.Domain.XFYun
|
||
{
|
||
public static class LogHelper
|
||
{
|
||
public static void Error(string msg)
|
||
{
|
||
Task.Run(() => WriteLogByTask(msg, 3));
|
||
}
|
||
|
||
public static void Error(Exception ex, string msg)
|
||
{
|
||
Task.Run(() => WriteLogByTask(msg, 4, ex));
|
||
}
|
||
|
||
public static void Error(string msg, Exception ex)
|
||
{
|
||
Task.Run(() => WriteLogByTask(msg, 4, ex));
|
||
}
|
||
|
||
public static void Info(string msg)
|
||
{
|
||
Task.Run(() => WriteLogByTask(msg, 1));
|
||
}
|
||
|
||
public static void Debug(string msg)
|
||
{
|
||
Task.Run(() => WriteLogByTask(msg, 3));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步调用
|
||
/// </summary>
|
||
/// <param name="msg"></param>
|
||
/// <param name="type">1:info 2: error 3: debug</param>
|
||
public static void WriteLogByTask(string msg, int type, Exception ex = null)
|
||
{
|
||
msg = encryPhone(msg);
|
||
switch (type)
|
||
{
|
||
case 1:
|
||
Log.Information(msg);
|
||
break;
|
||
|
||
case 2:
|
||
Log.Error(msg);
|
||
break;
|
||
|
||
case 3:
|
||
Log.Debug(msg);
|
||
break;
|
||
|
||
case 4:
|
||
Log.Error(ex, msg);
|
||
break;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 手机号正则匹配加密 手机号日志需以非数字开头 非数字结尾
|
||
/// </summary>
|
||
/// <param name="msg"></param>
|
||
/// <returns></returns>
|
||
public static string encryPhone(string msg)
|
||
{
|
||
try
|
||
{
|
||
// 使用正则表达式匹配手机号
|
||
string pattern = @"(\D\d{3})\d{4}(\d{4}\D.*)";
|
||
string replacement = "$1****$2";
|
||
//msg = Regex.Replace(msg, pattern, replacement);
|
||
Regex regex = new Regex(pattern);
|
||
//Match集合,匹配成功的字符串集合
|
||
MatchCollection collection = regex.Matches(msg);
|
||
|
||
//遍历Match集合,取出值
|
||
string telephone;
|
||
foreach (Match item in collection)
|
||
{
|
||
foreach (Group group in item.Groups)
|
||
{
|
||
telephone = group.Value.Trim();
|
||
//偶尔会出现重复提取,所以加了去重判断
|
||
msg = Regex.Replace(msg, pattern, replacement);
|
||
}
|
||
}
|
||
|
||
return msg;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error($"加密手机号错误{ex.Message}");
|
||
return msg;
|
||
}
|
||
}
|
||
}
|
||
} |