プラグインで ITracingService を使用する

カテゴリ: 保守性、サポート

影響の可能性: 中程度

症状

プラグインの問題や動作のデバッグとトラブルシューティングは、豊富で洞察に富んだログ記録やトレースがないと困難です。

Guidance

ITracingService インターフェイスは、実行時のカスタム情報を記録することで開発者を支援します。 この情報は、プラグインでのコードエラーや予期しない動作の原因の診断に役立ちます。トレース サービスに書き込む前に、まず、渡された実行コンテキストからトレース サービス オブジェクトを抽出します。 次に、必要に応じてカスタム コードに Trace 呼び出しを追加します。 そのメソッド呼び出しで、関連する診断情報を渡します。

Note

ITracingService インターフェイスを使用したトレース ログは、プラグインがサンドボックス モードで登録されている場合にのみ機能します。 実行時データを取得するには、トレース ログを有効にする必要があります。 詳細については、「 トレースとログ記録」を参照してください。

//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
    (ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
    serviceProvider.GetService(typeof(IPluginExecutionContext));

// For this sample, execute the plug-in code only while the client is online. 
tracingService.Trace("AdvancedPlugin: Verifying the client is not offline.");
if (context.IsExecutingOffline || context.IsOfflinePlayback)
    return;

// The InputParameters collection contains all the data passed 
// in the message request.
if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
{
    // Obtain the target entity from the Input Parameters.
    tracingService.Trace("AdvancedPlugin: Getting the target entity from Input Parameters.");
    Entity entity = (Entity)context.InputParameters["Target"];

    // Obtain the image entity from the Pre Entity Images.
    tracingService.Trace("AdvancedPlugin: Getting image entity from PreEntityImages.");
    Entity image = (Entity)context.PreEntityImages["Target"];
}

追加情報

トレースは、登録されたカスタム コードのトラブルシューティングに特に役立ちます。これは、そのシナリオでサポートされている唯一のトラブルシューティング方法であるためです。 トレースでは、同期または非同期の実行中に登録された sandboxed (部分信頼) カスタム コードがサポートされます。

参照

プラグインを記述する
トレースとログ記録