Auto log an exception c#
<p>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.</p>

<p>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).</p>

<p>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:</p>

<pre><code>public class Test
{
    ["TestMethod"]
    public void RunTest()
    {
        try
        {
            //run my code here
        }
        catch (Exception ex)
        {
            Logger.Error("Error message", ex);
            throw;
        }
    }
}
</code></pre>

<p>But I'm not sure using try-catch in order to log errors is a proper design.</p>

<p>I've thought of two things:</p>

<ol>
<li>Create a listener-thread which will catch my exceptions, log them
and re-throw them...</li>
<li>Manipulate the Exception class to use mt Log.Error() (or derive
MyException from it and manipulate my code to throw only
MyException).</li>
<li>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).</li>
</ol>

<p>Am I in the right way?
Are there any other ways to implement such an "auto write errors"?</p>

<p>Thanks,
Elad</p>
