0

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.

Cody
  • 8,686
  • 18
  • 71
  • 126

1 Answers1

2

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 or ConsoleAppender to log to the console
  • RollingFileAppender 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.

Nicholas Carey
  • 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