I'm writing an automation (test project) by vs2010. I already have a Logger class with the relevant Info(string), Debug(string) and Error(string, Exception) methods which implements the proper writing messages into file.
Now, I understand that my Info log has to be specifically written within my tests' code, but is there anyway to auto write the error messages as the implementation of the thrown exceptions? The exceptions are part of the design (I need to throw them in order to determine the pass/fail state of each test).
I can do the basic implementation of wrapping all my code in try-catch, write in the catch block the Logger.error(), then throw the exception again, like this:
public class Test
{
["TestMethod"]
public void RunTest()
{
try
{
//run my code here
}
catch (Exception ex)
{
Logger.Error("Error message", ex);
throw;
}
}
}
But I'm not sure using try-catch in order to log errors is a proper design.
I've thought of two things:
- Create a listener-thread which will catch my exceptions, log them and re-throw them...
- Manipulate the Exception class to use mt Log.Error() (or derive MyException from it and manipulate my code to throw only MyException).
- I've already tried to install the Enterprise-Library and using the logging-block, but I'm not sure this will suit my needs (and I've failed to get the instance of the LogWriter or use the Logger.Write anyway).
Am I in the right way? Are there any other ways to implement such an "auto write errors"?
Thanks, Elad