にもアクセスできることを忘れないでくださいITracingService
。
メソッドでそれへの参照を取得Execute
し、コードで頻繁に書き込み、試行中または成功した変数または一連のアクションをログに記録できます。また、例外が発生したときに、より価値のある情報を表示するためにも使用できます。
基本的には、コンソールへの書き込みに似ています。次に、何らかの原因で実行時にプラグインがクラッシュした場合、ユーザーに表示されたエラーで[ログ ファイルのダウンロード]をクリックすると、追跡したすべての情報を確認できます。
ただし、注意してください - プラグインが実際に例外をスローしない限り(故意またはその他の方法で)、トレースされたものにアクセスすることはできません。
例:
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(
typeof(IPluginExecutionContext));
// Get a reference to the tracing service.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
tracingService.Trace("Getting entity from InputParameters...");
// may fail for some messages, since "Target" is not present
var myEntity = (Entity)context.InputParameters["Target"];
tracingService.Trace("Got entity OK");
// some other logic here...
}
catch (FaultException<OrganizationServiceFault> ex)
{
_trace.Trace(ex.ToString());
while (ex.InnerException != null)
{
ex = (FaultException<OrganizationServiceFault>)ex.InnerException;
_trace.Trace(ex.ToString());
}
throw new InvalidPluginExecutionException(
string.Format("An error occurred in your plugin: {0}", ex));
}
catch (Exception ex)
{
_trace.Trace(ex.ToString());
while (ex.InnerException != null)
{
ex = ex.InnerException;
_trace.Trace(ex.ToString());
}
throw;
}
}