TG.WXCRM.V4/Common/LogHelper.cs

99 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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">1info 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));
}
}
}