【发布时间】:2017-02-12 08:26:14
【问题描述】:
我正在使用IParameterInspector 记录网络请求数据。我需要它来计算请求持续时间并记录输入和输出内容。
只要不发生异常,它就可以正常工作。如果出现异常,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;
}
}
}
【问题讨论】:
标签: c# .net wcf logging exception-handling