You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.6 KiB

using log4net;
using MethodDecorator.Fody.Interfaces;
using Rougamo;
using Rougamo.Context;
using System.Reflection;
using System.Text;
using BrilliantSightClient.Model.Extension;
namespace BrilliantSightClient.Model.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : MoAttribute
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(LogAttribute));
public override void OnEntry(MethodContext context)
{
//Logger.Debug($"Entering {context.Method.DeclaringType?.Name}.{context.Method.Name}");
// 获取方法参数
// var parameters = context.Method.GetParameters();
// var arguments = context.Arguments;
//
// // 构建参数字符串
// var parameterLog = new StringBuilder();
// for (int i = 0; i < parameters.Length; i++)
// {
// parameterLog.Append($"{parameters[i].Name} = {arguments[i]}, ");
// }
// 记录日志
//Logger.Debug($"Entering {context.Method.DeclaringType?.Name}.{context.Method.Name} with parameters: {parameterLog}");
}
public override void OnExit(MethodContext context)
{
//Logger.Debug($"Exiting {context.Method.DeclaringType?.Name}.{context.Method.Name}");
var returnValue = context.ReturnValue;
// 获取返回值
if (context.Exception != null)
{
var parameters = context.Method.GetParameters();
var arguments = context.Arguments;
// 构建参数字符串
var parameterLog = new StringBuilder();
for (int i = 0; i < parameters.Length; i++)
{
parameterLog.Append($"{parameters[i].Name} = {arguments[i].ToSafeAbundantString()}, ");
}
Logger.Error($"[METS]=============Method Exception Termination[MsgStart]=============");
Logger.Error($"Method: {context.Method.DeclaringType?.Name}.{context.Method.Name}");
Logger.Error($"Method Parameters: {parameterLog.ToString()}");
Logger.Error($"Exception: {context.Exception.Message} \r\n{context.Exception.StackTrace}");
Logger.Error($"[METE]=============Method Exception Termination[MsgEnd]===============");
}
else
{
// 记录日志
//Logger.Debug($"[MethodExiting] Exiting {context.Method.DeclaringType?.Name}.{context.Method.Name} with return value: {returnValue}");
}
}
}
}