我只想用一些 id 绑定 WCF 传入和传出消息,将记录到数据库的内容。
由于它计划在高多线程环境中使用,因此出现了一些问题。
后编辑
这是我想要记录的方式:
public class LogMessageInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
Dictionary<string, object> logParams = (Dictionary<string, object>)correlationState;
logParams["description"] = reply.ToString();
Logger log = LogManager.GetCurrentClassLogger();
log.InfoEx(String.Format("response_{0}", logParams["_action"]), logParams);
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
string iteration_id = "";
// here comes seeking for custom, previously setted header with id
for (int i = 0; i < request.Headers.Count; i++)
{
if ((request.Headers[i].Name == "_IterationId") && (request.Headers[i].Namespace == "http://tempuri2.org"))
{
iteration_id = request.Headers.GetHeader<string>(i);
request.Headers.RemoveAt(i);
break;
}
}
string pair_id = StringGenerator.RandomString(10);
string action_name = request.Headers.Action.Substring(request.Headers.Action.LastIndexOf("/") + 1);
Dictionary<string, object> logParams = new Dictionary<string,object>() { {"iteration_id", iteration_id}, { "description", request.ToString() }, { "request_response_pair", pair_id }, { "_action", action_name } };
Logger log = LogManager.GetCurrentClassLogger();
log.InfoEx(String.Format("request_{0}", action_name), logParams);
return logParams;
}
}