Enterprise Library のログ記録は、いじる必要のある多くのビットを提供します。では、構成を変更する前に、すぐに使用できる設定のいくつかで、やりたいことを実行できるのではないでしょうか?
アプリケーションが起動時にいくつかのメッセージをログに記録し、それ以上メッセージをログに記録しないようにしたいように思えます。起動メッセージのログ記録がいつ完了したかを明示的に知っていると仮定しています。
上記が当てはまる場合、最も簡単な方法は LogEntry Priority プロパティを使用することだと思います。
構成ファイルで、最小優先度 2 の優先度フィルターを設定します。
<logFilters>
<add minimumPriority="2" maximumPriority="2147483647" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
name="Priority" />
<add enabled="true" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
name="LogEnabled Filter" />
</logFilters>
次に、起動時に優先度 2 (またはそれ以上) を使用してメッセージをログに記録します。次に、完了したことがわかったら、優先度を 1 に下げます。
私はそれをよりよくカプセル化しますが、コードサンプルは次のようになります:
public class MyApp
{
public static int LogPriority = 2;
public static readonly string MyAppCategory = "MyAppCategory";
static void Main()
{
Logger.Write("Loading 1...", MyAppCategory, LogPriority);
// ...
Logger.Write("Loading 2...", MyAppCategory, LogPriority);
// Done loading so turn off logging
LogPriority = 1;
Logger.Write("Message not logged", MyAppCategory, LogPriority);
}
}
アプリの残りのログを有効にしたい場合は、優先度フィルターを 2 から 1 に下げるだけで、すべてがログに記録されます。