ファーストステップ:
- それは
存在しない場合は、キューを作成します - 非同期
便利なキューにメッセージをプッシュ: 窓はその後、サーバ/ PC上
を特色としてMSMQsをインストールします。 MSMQからメッセージをプッシュして取得するためのguide
コード例:
public class ExceptionMSMQ
{
private static readonly string description = "Example description";
private static readonly string path = @".\Private$\myqueue";
private static MessageQueue exceptionQueue;
public static MessageQueue ExceptionQueue
{
get
{
if (exceptionQueue == null)
{
try
{
if (MessageQueue.Exists(path))
{
exceptionQueue = new MessageQueue(path);
exceptionQueue.Label = description;
}
else
{
MessageQueue.Create(path);
exceptionQueue = new MessageQueue(path);
exceptionQueue.Label = description;
}
}
catch
{
throw;
}
finally
{
exceptionQueue.Dispose();
}
}
return exceptionQueue;
}
}
public static void PushMessage(string message)
{
ExceptionQueue.Send(message);
}
private static List<string> RetrieveMessages()
{
List<string> messages = new List<string>();
using (ExceptionQueue)
{
System.Messaging.Message[] queueMessages = ExceptionQueue.GetAllMessages();
foreach (System.Messaging.Message message in queueMessages)
{
message.Formatter = new XmlMessageFormatter(
new String[] { "System.String, mscorlib" });
string msg = message.Body.ToString();
messages.Add(msg);
}
}
return messages;
}
public static void Main(string[] args)
{
ExceptionMSMQ.PushMessage("my exception string");
}
}
これを行うために広く使用されている他の方法は、エンタープライズライブラリやNLogのようなこの機能を既に備えている簡単なインターフェイスを提供する、すぐに使用できるロガーを使用することです。
メッセージを取得するには、定期的にメッセージを読んで処理する別のWindowsサービスをお勧めします。それを行う方法の良い例がここに与えられます。Windows service with timer
更新: Windowsサービス例:
MSMQConsumerService.cs
public partial class MSMQConsumerService : ServiceBase
{
private System.Timers.Timer timer;
public MSMQConsumerService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.timer = new System.Timers.Timer(30000D); // 30000 milliseconds = 30 seconds
this.timer.AutoReset = true;
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ProcessQueueMessages);
this.timer.Start();
}
protected override void OnStop()
{
this.timer.Stop();
this.timer = null;
}
private void ProcessQueueMessages(object sender, System.Timers.ElapsedEventArgs e)
{
MessageProcessor.StartProcessing();
}
}
とMessageProcessor.csを
public class MessageProcessor
{
public static void StartProcessing()
{
List<string> messages = ExceptionMSMQ.RetrieveMessages();
foreach(string message in messages)
{
//write message in database
}
}
}
出典
2016-11-23 07:46:17
Pan
あなたはそれぞれのコードであなたの答えを詳しく教えてください。 –
@ B.バラマニガンダンここにあなたは仲間です。あなたを助けるためのコードをいくつか追加しました。私は物事がより明確になることを願っています – Pan
かなり良い...私はWindowsサービス経由で読書方法を呼び出す必要があります。このサービスは、読み取りメソッドを実行するための呼び出しを開始する必要があります。 - その部分であなたの考えを親切に分かち合う。 –