问题
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?
回答1:
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));
回答2:
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)
来源:https://stackoverflow.com/questions/16693353/can-i-write-the-results-of-simpleinjectors-diagnostics-to-a-log-file