AOP编程

AOP工具类
public class AroundAdvice : IMethodInterceptor
{
bool logurl = true;
public AroundAdvice()
{
Task.Run(() => {
logurl = WebUtils.UrlValidateCheck(SystemAddress.LogServiceUrl);
});
}
public object Invoke(IMethodInvocation invocation)
{
string guid = Guid.NewGuid().ToString();
object result = null;
string actionName = invocation.TargetType.FullName + "/" + invocation.Method.Name;
string strMessageStart = string.Format("开始-唯一标识:{0},类名:{1},方法名:{2},传参:{3}", guid, invocation.TargetType.FullName, invocation.Method.Name, Newtonsoft.Json.JsonConvert.SerializeObject(invocation.Arguments));
try
{
WriteLog(invocation.TargetType.ToString(), actionName, strMessageStart);

result = invocation.Proceed();

string strMessageEnd = string.Format("结束-唯一标识:{0},类名:{1},方法名:{2},返回结果——请求:{3}", guid, invocation.TargetType.FullName, invocation.Method.Name, "Response:" + Newtonsoft.Json.JsonConvert.SerializeObject(result) + ",Request:" + Newtonsoft.Json.JsonConvert.SerializeObject(invocation.Arguments));
WriteLog(invocation.TargetType.ToString(), actionName, strMessageEnd);
}
catch (Exception ex)
{ }


return result;
}

 

/// <summary>
/// 日志记录方法
/// </summary>
/// <param name="context"></param>
/// <param name="actionName"></param>
/// <param name="massage"></param>
public void WriteLog(string messageType,string actionName, string massage)
{
var log = LogFactory.GetLogger(messageType);
//bool logurl = WebUtils.UrlValidateCheck(SystemAddress.LogServiceUrl);
if (logurl)
{
Task.Run(() =>
{
try
{
ExFreshLog.Instance.Info(actionName, massage + ";IP:" + Net.Ip);
}
catch (Exception ex)
{
// log.InfoByLocal(massage);
}
});
}
// else
// log.InfoByLocal(massage);
}
}

posted @ 2017-09-23 18:43  干饭人~  阅读(122)  评论(0编辑  收藏  举报