我正在使用PostSharp制作处理方法记录的自定义属性,然后将其写入数据库表。
每件事情都很好,但OnSuccess每次都在工作!即使我有例外!! 我对OnSuccess的想法是:只有在没有异常的情况下才能工作!
我做错了什么?public class Program
{
static void Main(string[] args)
{
MOHE.Program.Test test = new MOHE.Program.Test();
Log x = new Log();
test.testmethod(111 , 222 , "AAA");
}
}
这是测试类:
public class Test
{
[LogAttribute]
public int testmethod(int x , int y , string z)
{
if (x == 2)
x = 5;
else throw new Exception("Exception");
return x;
}
这是我的自定义属性:
[Serializable]
public sealed class LogAttribute : PostSharp.Aspects.OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Logger logger = new Logger();
DateTime dateTime = DateTime.Now;
string Parameters = "";
for (int i = 0; i < args.Method.GetParameters().Length; i++)
{
Parameters += args.Arguments[i].ToString() + " , ";
}
Log LOG = new Log
{
LogMassg = " Method Entry ",
LogStatus = 1,
LogType = 1,
CreatedDate = dateTime,
InputParameters = Parameters,
MethodName = (args.Method.Name).ToString(),
ClassName = (args.Method.DeclaringType).ToString(),
};
logger.AddLog(LOG);
Trace.WriteLine(string.Format("Entering {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), LOG.LogMassg);
}
public override void OnException(MethodExecutionArgs args)
{
Logger logger = new Logger();
DateTime dateTime = DateTime.Now;
string Parameters = "";
for (int i = 0; i < args.Method.GetParameters().Length; i++)
{
Parameters += args.Arguments[i].ToString() + " , ";
}
Log LOG = new Log
{
LogMassg = " Exception ",
LogStatus = 2,
LogType = 4,
CreatedDate = dateTime,
InputParameters = Parameters,
MethodName = (args.Method.Name).ToString(),
ClassName = (args.Method.DeclaringType).ToString(),
};
logger.AddLog(LOG);
Trace.WriteLine(string.Format("Exception {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), LOG.LogMassg);
}
public override void OnSuccess(MethodExecutionArgs args)
{
Logger logger = new Logger();
DateTime dateTime = DateTime.Now;
string Parameters = "";
for (int i = 0; i < args.Method.GetParameters().Length; i++)
{
Parameters += args.Arguments[i].ToString() + " , ";
}
Log LOG = new Log
{
LogMassg = " Method Execution Success ",
LogStatus = 1,
LogType = 4,
CreatedDate = dateTime,
InputParameters = Parameters,
ReturnValues = (args.ReturnValue ?? "Exception occurred").ToString(),
MethodName = (args.Method.Name).ToString(),
ClassName = (args.Method.DeclaringType).ToString(),
};
logger.AddLog(LOG);
Trace.WriteLine(string.Format("Method Execution Success {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), LOG.LogMassg);
}
public override void OnExit(MethodExecutionArgs args)
{
Logger logger = new Logger();
DateTime dateTime = DateTime.Now;
string Parameters = "";
for (int i = 0; i < args.Method.GetParameters().Length; i++)
{
Parameters += args.Arguments[i].ToString() + " , ";
}
Log LOG = new Log
{
LogMassg = " Method Exit ",
LogStatus = 1,
LogType = 1,
CreatedDate = dateTime,
InputParameters = Parameters,
ReturnValues = (args.ReturnValue ?? "Exception occurred").ToString(),
MethodName = (args.Method.Name).ToString(),
ClassName = (args.Method.DeclaringType).ToString(),
};
logger.AddLog(LOG);
Trace.WriteLine(string.Format("Leaving {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), LOG.LogMassg);
}
}
数据库记录:
LogID LogMassg InputParameters ReturnValues
97 Method Entry 111 , 222 , AAA NULL
98 Exception 111 , 222 , AAA , NULL
99 Method Execution Success 111 , 222 , AAA , 0
100 Method Exit 111 , 222 , AAA , 0