私たちはIoCコンテナとしてNinjectバージョン2を使用し始めました。 Log4netをロギングに使用しています。ログの問題を解決した依存関係アプリケーションの起動
私が望むのは、Ninjectが見つけたすべての依存関係と、それを解決するためのもの、できればアプリケーションの起動時のログを記録することです。
私はロギング拡張を見つけましたが、これを取得するための使用方法に関するドキュメントまたは例は見つかりません。
編集:
それがここで要求されたためではlog4netの
パブリッククラスDefaultBindingGeneratorWithLogging使用して、起動時にデフォルトのバインディングをログに記録したクラスです。IBindingGenerator { プライベート読み取り専用のiKernelカーネルを、
/// <summary>
/// Initializes a new instance of the <see cref="DefaultBindingGeneratorWithLogging"/> class.
/// </summary>
/// <param name="kernel">The kernel.</param>
public DefaultBindingGeneratorWithLogging(IKernel kernel)
{
this.kernel = kernel;
}
/// <summary>
/// Creates the bindings for a type.
/// </summary>
/// <param name="type">The type for which the bindings are created.</param>
/// <param name="bindingRoot">The binding root that is used to create the bindings.</param>
/// <returns>
/// The syntaxes for the created bindings to configure more options.
/// </returns>
public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot)
{
if (type.IsInterface || type.IsAbstract)
{
yield break;
}
Type interfaceForType = type.GetInterface("I" + type.Name, false);
if (interfaceForType == null)
{
yield break;
}
var log = kernel.Get<ILog>();
if (!(kernel.GetBindings(interfaceForType).Any()))
{
bindingRoot.Bind(interfaceForType).To(type).InTransientScope();
if (log.IsInfoEnabled && !String.IsNullOrWhiteSpace(interfaceForType.FullName))
{
log.InfoFormat("Mapping {0} -> {1}", type.FullName, interfaceForType.FullName);
}
}
else
{
log.InfoFormat("Didn't map {0} -> {1} mapping already exists", type.FullName, interfaceForType.FullName);
}
}
}
注:これは、バインディングがアクティブ化/非アクティブ化されたときにログに記録します。起動時にすべてのログを記録するのとは少し異なりますが、おそらく有用でしょうか? – rcravens
非常に便利です。私は、コンベンションベースのバインディングをログに記録するためのコードを用意しています。自分のクラスを見つけたときにログを記録するようにしていますが、アクティベーション/ディアクティベートもログに記録したいのですばらしいです。 – Mant101
@ Mant101、あなたがこの問題のために偉大で役立つ完全性のための慣例ベースのバインディングロギングのためのコードを共有することができれば。 – user3141326