Is there anything like the Enterprise Library Patterns & Practices for use in console applications? I'm specifically looking for something to use for logging / email notification on exceptions instead of calling a SendExceptionMail
function on every catch
block.

- 8,686
- 18
- 71
- 126
-
What's wrong with the Enterprise Library for Console Applications? – nvoigt Apr 12 '13 at 17:48
-
I could be wrong, but I was under the impression that it only worked with web applications? – Cody Apr 12 '13 at 17:49
-
1I'm not sure about Web Applications, but it works quite well with Console or Windows Apps. – nvoigt Apr 12 '13 at 17:51
-
Didn't realize that, will have to investigate – Cody Apr 16 '13 at 19:47
1 Answers
I use log4Net for this sort of thing, I generally configure the executable with multiple appenders and dispense with Console.Write()
altogether. I generally have (at least) a
ColoredConsoleAppender
orConsoleAppender
to log to the consoleRollingFileAppender
to create a log file.
Just add an SmtpAppender
to send events via email. Add other appenders to log to the Windows event log or a SQL database as needed.
See http://logging.apache.org/log4net/release/config-examples.html for some config examples.
As an aside...why do you need multiple catch
blocks? You shouldn't be catch
-ing anything unless you're actually handling the exception — though there are some exceptions: I'd recommend catching, logging and continuing exceptions at the points at which process or machine boundaries are crossed.
Instead, let exceptions bubble up. Have a single catch
in Main()
that logs the exceptions. Much simpler.

- 71,308
- 16
- 93
- 135
-
+1 - You shouldn't need an email unless an unexpected error occurs (unless you want emails for everyday occurrences). And unexpected errors shouldn't occur. So, you probably only need to generate emails inside one catch block. – qxn Apr 12 '13 at 19:36