Search code examples
dryioc

DryIoc RegisterInitializer for logging resolution errors?


Looking at the documentation for DryIoc I see there is a way to register an initializer func that could be used to log successful resolutions.

Is there anyway to log failed resolutions using RegisterInitializer or similar?


Solution

  • Currently there is no direct way to log every possible error per resolution, except by catching the ContainerException.

    But it is possible to detect potential errors beforehand:

    var errors = container.VerifyResolutions();
    

    Here is the docs.

    If you interested in unknown registrations you can add the rule to log unknown via UnknownServiceResolvers:

    container = new Container(rules =>
        rules.WithUnknownServiceResolvers(
           request => {
               Log.Error("Not found: " + request);
               return null;
           }));
    

    Possibly, there are other ways: for instance using Decorators to wrap allow-default services. But it strongly depends on your context.