2

I have a class in c# to help me log errors (ErrorClass).

  • The class has 3 methods. Log Error to: File System, Windows Event, Email.
  • 2 of the 3 methods require settings like "to email", or "directory path".
  • Settings are stored in the registry
  • I use dependency injection to instantiate the RegistryClass inside the ErrorClass .

This is how I instantiate the ErrorHandle Class inside the Registry Class

ErrorHandle _ErrorHandle = new ErrorHandle();

And here is how I instantiate the Registry Class inside the ErrorHandle Class

RegistryTools _GetRegistry = new RegistryTools();

I have a class to help me retrieve values from the registry (RegistryClass)

  • The registry class needs to handle errors
  • I use dependency injection to instantiate the errorClass inside the RegistryClass

When I use dependency injection in both classes, an Endless LOOP is created when there is an error.

What is the suggested way or best practice of handling this situation:

  • Should I access the registry inside the ErrorClass?
  • Should I not ErrorHandle the RegistryClass?
  • Should I create a separate ErroHandle procedure for the RegistryClass?
MataHari
  • 318
  • 3
  • 6
  • 15
  • 1
    "best practice" is to have your logger depend on as few external services as possible. After all, you want yo logger to be available at all times. I somehow doubt registry is such a crucial dependency for logging - at least, you should consider fallback solution – driushkin Mar 05 '12 at 06:08
  • You are correct. I revised the title of the question – MataHari Mar 07 '12 at 14:32

2 Answers2

0

Don't re-invent this wheel. There is a tried and tested open source logging framework for .NET available, log4net. If you feel the need to use DI with it, you can do that too. log4net uses an XML file for configuration, which is much more accessible and less fraught with peril than dealing with the registry. It also swallows its own errors and makes them accessible via a debugging trace.

Community
  • 1
  • 1
dbugger
  • 15,868
  • 9
  • 31
  • 33
  • I get paid to reinvent the wheel. Now back to my question, what would you do? Thanks – MataHari Mar 05 '12 at 02:39
  • I was not asking to re-write logging code. Nor the question is about logging. The question is about best practices to avoid infinite loops. – MataHari Mar 05 '12 at 02:58
  • 1
    I do apologize for answering the question you should have asked. Carry on. – dbugger Mar 05 '12 at 03:07
  • You sould amor apoligize fo being ignorant. Log4net does not solve all problems - I partially have my own logging framework, too, as part of an application framework. It sends most stuff forware to Log4Net, but it allows replacement of lo4gnet when needed. And some "errors" are not logging level errors but data level (i.e. program works, a set of data is marked as in error). – TomTom Mar 05 '12 at 05:56
  • 1
    If something does most of what you need, but not all, then **extend** it, don't *rewrite* it. If it does nothing you want, then find something else that does (most likely something is already out there), otherwise as a *last resort*, roll your own. I find it hard to believe this situation falls in the latter category. – RPM1984 Mar 05 '12 at 05:58
0

What mechanism are you using for DI? If you use setter injection there should be nothing to stop you doing something like:

var logger = new ErrorClass();
var registry = new RegistryClass();
logger.Registry = registry;
registry.Logger = logger;
Ian Horwill
  • 2,957
  • 2
  • 24
  • 24