Simple Injectorを使用して、ロガーが注入されるサービス オブジェクトのコンストラクターにロガー名を設定するという、ロガーに共通のテーマに遭遇しました。サービスがログに書き込むときは、このログ名で簡単に識別できます。
LogNames
はサービスごとに設定されるため、ロガーはオブジェクト グラフ リクエストごとに一意である必要があります。
グラフが作成されているときにこれを自動的に実行したいのですが、いろいろ調べてみましExpressionBuilt()
たが、まだ作業したいものを得るのに苦労しています.
私のコンストラクター コードは以下のとおりです (このLogName
プロパティ設定コードは、ほとんどのサービスで共通です)。
ありがとう、
クリス
public interface ILogger
{
void LogMessage(string message, LogLevel level,
ILoggableCompany company = null);
string LogName {get; set; }
}
public BusinessUnitService
{
private readonly IUnitOfWork unitOfWork;
private readonly ILogger logger;
public BusinessUnitService(IUnitOfWork unitOfWork,
ILogger logger)
{
this.unitOfWork = unitOfWork;
this.logger = logger;
// it would be great if we could take away this
// line and set it automatically
this.logger.LogName = this.GetType().ToString();
}
}