Reputation: 15
I am using PostSharp to making custom attributes that handling method logging , then write it in database table .
every thing it's working fine , but OnSuccess working every time ! even when i have an exception !! my thought about OnSuccess is : working only if there is no Exception !
am i doing something wrong ?
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");
}
}
and this is mt Test Class :
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;
}
and this is my custom attribute :
[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);
}
}
Database records :
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
Upvotes: 1
Views: 497