C# アプリケーションの独自のログ サービスに既存のログ ライブラリをラップして、特定のログ状況用に定義済みのメソッドで囲みます。
public class LoggingBlockLoggingService : ILoggingService
{
private LogWriter writer;
public LoggingBlockLoggingService(LogWriter writer)
{
this.writer = writer;
}
....//logging convenience methods, LogWarning(), etc...
}
この実装を変更して、インスタンス化するクラスの Type を受け取るようにしたいと思います (この場合、基になるロガー、LogWriter はシングルトンになります)。したがって、この実装 (およびインターフェイス ILoggingService) を一般的なものにします。
public class LoggingBlockLoggingService<T> : ILoggingService<T>
{
...
private string typeName = typeof(T).FulName;
...
または、追加のコンストラクタ パラメータを追加します。
public class LoggingBlockLoggingService : ILoggingService
{
private LogWriter writer;
private string typeName;
public LoggingBlockLoggingService(LogWriter writer, Type type)
{
this.writer = writer;
this.typeName = type.FullName;
}
....//Include the typeName in the logs so we know the class that is logging.
}
タイプを登録するときに Unity でこれを一度設定する方法はありますか? ログに記録したいすべてのクラスにエントリを追加する必要はありません。理想的には、誰かが私たちのプロジェクトのクラスにロギングを追加したい場合、彼らが取り組んでいる各クラスを登録するために Unity 構成に別の行を追加する代わりに、彼らが取り組んでいるクラスのコンストラクタに ILoggingService を追加するだけです。 .
XMLではなく、ランタイム/コード構成を使用しています