views:

25

answers:

2

The other day, a co-worker created a simple interface to report errors/messages

 public interface IErrorReporter
 {
  void ReportError(string title, string message, string detail);
 }

The goal is to keep lower-level "business logic" code free of UI such as MessageBox.Show().

My reaction is there has to be something already "out there" to help with this, but I can't really find anything. Note that I'm not looking for a fancy error reporting, exception-handling mechanism; just something "out of the box" to avoid/reduce creating/implementing my own interface.

A custom trace listener looks like one alternative, but System.Diagnostics.Trace just doesn't feel like the right alternative to MessageBox.Show(). Using a third-party solution such as log4net seems like overkill for four lines.

+1  A: 

Have you tried looking at log4net? I use it when I can and love it - it is quite configurable, and you can write custom appenders that do things like output to a dialog, and the appenders are configured via config file - so your "busines logic" code just logs things like errors/warnings/info, whatever, and your configuration says what to do with that information.

From code it's simple:

logger.Debug("Debug stuff");
logger.Info("Info Stuff");
logger.Error("Error Stuff"); //or warn, or fatal

And the config is fairly easy:

<configuration>
    <configSections>
        <section name="log4net" 
           type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <appender name="LogFile" type="log4net.Appender.FileAppender">
            <param name="File" value="output.txt" />
            <param name="AppendToFile" value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <param name="Header" value="[Header]\r\n" />
                <param name="Footer" value="[Footer]\r\n" />
                <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
            </layout>
        </appender>

        <!-- more appenders if you want -->

        <root>
            <level value="INFO" />
            <appender-ref ref="LogFile" />            
        </root>
    </log4net>
</configuration>
Philip Rieck
I've seen multiple references to `log4net`; I've got nothing against trying it, but it seems like overkill.
Dan
Something already "out there" will certainly be more general purpose than something you build specificly for your interface. So yes, it will have a bunch of stuff you don't need. If you don't want the additional capability, why not just implement your interface and pop up a modal?
Philip Rieck
A: 

Why not expand the interface to wrap around log4net and just configure the appenders accordingly? The NET SEND appender should be able to mimic the MessageBox.Show() functionality fairly closely, and at the same time other appenders are logging the messages to a database, file, event log, etc.

David