Using SimpleInjector I call container.Verify() at the end of my configuration, and get the diagnostics information in the debugger as described in the documentation. I would like to write that information to a log file. Is there a way access it programmatically or a way hook a logger or tracer into SimpleInjector?

有帮助吗?

解决方案

Simple Injector 2.4 contains a diagnostic API (SimpleInjector.Diagnostics.dll) that allow you to query the container for diagnostic warnings. Using this API you can write integration tests that automatically check the configuration for diagnostic warnings:

// using SimpleInjector.Diagnostics;

[TestMethod]
public void Container_Always_ContainsNoDiagnosticWarnings()
{
    // Arrange
    var container = Bootstrapper.GetInitializedContainer();

    container.Verify();

    // Assert
    var results = Analyzer.Analyze(container);

    Assert.IsFalse(results.Any(), Environment.NewLine +
        string.Join(Environment.NewLine,
            from result in results
            select result.Description));
}

Of course, you can also write this to a file:

var results = Analyzer.Analyze(container);

File.WriteAllLines("c:\\diagnostic.txt", results.Select(r => r.Description));

其他提示

ContainerDebugView which I guess can be categorized as the Facade class for the Diagnostics mechanism in SimpleInjector is only used as a DebuggerTypeProxy for the Container class.

It's an internal class with a public constructor so you can't construct it without using reflection and the Activator class. If you do that however you should be able to call the constructor with your container and then use the Items property to get an array of DebuggerViewItem which contains the Name, Description etc. of warnings and errors in your configuration.

Hope this helps as I can certainly see why you might want to be able to view the errors without using the debugger. (Eg. in Unit-Tests)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top