2014-07-25 44 views
1

我正在嘗試將來自服務的請求響應記錄到文本文件中。我正在使用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\\[email protected]\\" + "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(); 

     } 
    } 
+2

你可以換'fs'和'writer'在'using'塊並只是順便爲你的'finally'擺脫的需要。您還將清理文件,這對於這樣的應用程序聽起來很重要。 –

+0

@MatthewHaugen - 好的,我會照顧的。 –

回答

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; 
     } 
    } 
+0

我不確定這個例子。我有一個擴展類的原始服務參考類。 –

+0

這樣做後,我開始應用程序時給我一個錯誤。 –