我正在尝试将来自服务的请求响应记录到文本文件中。我使用log4net将响应记录到文本文件,但它只记录请求而不记录响应。 我怎么能这样做?
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
if (log.IsErrorEnabled)
{
SaveResponseToLog("AfterReceiveReply" +"\n##### VisService SOAP Response #####\n");
string filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Daimler AG\\Vis@\\" + "log.txt";
System.Xml.XmlDictionaryWriter writer = null;
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
reply = buffer.CreateMessage();
System.ServiceModel.Channels.Message replyCopy = buffer.CreateMessage();
try
{
FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
writer = System.Xml.XmlDictionaryWriter.CreateTextWriter(fs);
replyCopy.WriteMessage(writer);
MessageBox.Show("AfterReceiveReply" + writer.ToString());
writer.Flush();
}
catch (Exception e)
{
//Something went wrong while writing the response
MessageBox.Show(e.Message);
}
finally
{
writer.Close();
}
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
{
if (log.IsErrorEnabled)
{
string visServiceSOAPRequest = request.ToString();
log.Error("Error:VisService SOAP Request >>>");
log.Error(visServiceSOAPRequest);
//For displaying the message in the mail confirmation box
SaveResponseToLog("BeforeSendRequest" + "\nVisService SOAP Request >>>\n" + visServiceSOAPRequest);
}
return null;
}
private void SaveResponseToLog(string message)
{
System.IO.StreamWriter writer = null;
//StreamReader rd = null;
string filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\My App\\Application\\" + "log.txt";
if (!File.Exists(filename))
{
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
fs.Flush();
fs.Close();
}
}
try
{
if (File.Exists(filename))
{
writer = System.IO.File.AppendText(filename);
writer.Write(message);
writer.Flush();
}
}
catch (Exception e)
{
// Wenn das loggen nicht funktioniert, kann der Fehler ja nicht geloggt werden..
log.Error("Error saving the message in the log.txt file: " + e.Message);
}
finally
{
writer.Close();
}
}
答案 0 :(得分:0)
以下代码只适合我。按属性[MyServiceBehavior]
装饰您的服务实现可以在AfterReceiveRequest方法中看到请求。
可以在BeforeSendReply方法中看到响应。
public class MyServiceBehaviorAttribute : Attribute, IServiceBehavior
{
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
{
epDisp.DispatchRuntime.MessageInspectors.Add(new Inspector());
foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations)
op.ParameterInspectors.Add(new Inspector());
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
public class Inspector : IDispatchMessageInspector, IParameterInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine("IDispatchMessageInspector.AfterReceiveRequest called.");
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
Console.WriteLine("IDispatchMessageInspector.BeforeSendReply called.");
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
Console.WriteLine(
"IParameterInspector.AfterCall called for {0} with return value {1}.",
operationName,
returnValue.ToString()
);
}
public object BeforeCall(string operationName, object[] inputs)
{
Console.WriteLine("IParameterInspector.BeforeCall called for {0}.", operationName);
return null;
}
}