99 lines
2.8 KiB
C#
99 lines
2.8 KiB
C#
using log4net;
|
||
using System;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace WX.CRM.Common
|
||
{
|
||
public class LogHelper
|
||
{
|
||
public static ILog log = LogManager.GetLogger(typeof(LogHelper));
|
||
|
||
public static void Error(Exception ex)
|
||
{
|
||
log.Error(ex);
|
||
}
|
||
|
||
public static void Error(string msg)
|
||
{
|
||
Task.Run(() => WriteLogByTask(msg, 2));
|
||
}
|
||
|
||
public static void Error(string msg, Exception ex)
|
||
{
|
||
log.Error(msg, ex);
|
||
}
|
||
|
||
public static void Info(string msg)
|
||
{
|
||
Task.Run(() => WriteLogByTask(msg, 1));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步调用
|
||
/// </summary>
|
||
/// <param name="msg"></param>
|
||
/// <param name="type">1:info 2: error 3: debug</param>
|
||
public static void WriteLogByTask(string msg, int type)
|
||
{
|
||
msg = encryPhone(msg);
|
||
switch (type)
|
||
{
|
||
case 1:
|
||
log.Info(msg);
|
||
break;
|
||
|
||
case 2:
|
||
log.Error(msg);
|
||
break;
|
||
|
||
case 3:
|
||
log.Debug(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;
|
||
}
|
||
}
|
||
|
||
public static void Debug(string msg)
|
||
{
|
||
Task.Run(() => WriteLogByTask(msg, 3));
|
||
}
|
||
}
|
||
} |