2009-05-06 51 views
3

我目前正在C#中使用MSMQ進行批處理應用程序。在應用程序設計中,我有一個包含使用ActiveXFormatter的XML消息的錯誤隊列。我知道我可以編寫一個應用程序將這些錯誤消息寫入文本文件或數據庫表。有沒有一種工具可以輕鬆地從消息隊列(MSMQ)中導出消息?

是否有其他預建可用的工具允許您將消息導出爲各種格式(即文本文件,數據庫表等)?我只是在尋找最佳實踐。

回答

4

好的。我發現編寫代碼的解決方案非常簡單。這是我的參考解決方案。

 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Messaging; 

namespace ExportMSMQMessagesToFiles 
{ 
    public partial class frmMain : Form 
    { 
     public frmMain() 
     { 
      InitializeComponent(); 
     } 

     private void btnExportTextFiles_Click(object sender, EventArgs e) 
     {   
      //Setup MSMQ using path from user... 
      MessageQueue q = new MessageQueue(txtMSMQ.Text); 

      //Setup formatter... Whatever you want!? 
      q.Formatter = new ActiveXMessageFormatter(); 

      // Loop over all messages and write them to a file... (in this case XML) 
      MessageEnumerator msgEnum = q.GetMessageEnumerator2(); 
      int k = 0; 
      while (msgEnum.MoveNext()) 
      { 
       System.Messaging.Message msg = msgEnum.Current;         
       string fileName = this.txtFileLocation.Text + "\\" + k + ".xml";     
       System.IO.File.WriteAllText(fileName, msg.Body.ToString()); 
       k++; 
      } 

      MessageBox.Show("All done!"); 
     } 
    } 
} 

相關問題