私はIParameterInspectorを使用してウェブリクエストデータを記録しています。私は要求の期間を計算し、また入力と出力の内容を記録する必要があります。WCFサービスでIParameterInspectorを使用して、例外の場合でもWeb要求をログに記録する方法
例外が発生しない限り正常に動作します。例外の場合、AfterCallイベントは決して実行されません。
例外が発生したときにAfterCallを実行する機会はありますか?あるいは私はここに別のアプローチが必要ですか?
ここに私のコードの簡易版:
using System;
using System.ServiceModel.Dispatcher;
namespace WcfService1
{
public class LogOperationInspector : IParameterInspector
{
private string _serviceName;
public LogOperationInspector(string serviceName)
{
this._serviceName = serviceName;
}
public object BeforeCall(string operationName, object[] inputs)
{
return LogCallModel.GetLogModel("MyTestOperation", inputs);
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
var callModel = correlationState as LogCallModel;
callModel.EndDate = DateTime.Now;
// Log Start Date and End Date and calculate duration
}
}
public class LogCallModel
{
public String OperationName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public static object GetLogModel(string operationName, object[] inputs)
{
var result = new LogCallModel();
result.OperationName = operationName;
result.StartDate = DateTime.Now;
// Add some more properties for logging from the inputs field
return result;
}
}
}