First step: Install MSMQ as a Windows function on the server / PC
Then:
- Create a queue if it does not exist
- asynchronously press the message in the queue
Useful guide
Sample code for pushing and receiving messages from msmq:
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"); } }
Another widely used way to do this will also be to use from mailbox logs that already contain this functionality, such as Enterprise Library or NLog, which provide simple interfaces for this.
To receive messages, I would recommend a separate Windows service that periodically read messages and processed them. A good example of how to do this is given here: a Windows service with a timer
Update: Windows Service Example:
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);
and MessageProcessor.cs
public class MessageProcessor { public static void StartProcessing() { List<string> messages = ExceptionMSMQ.RetrieveMessages(); foreach(string message in messages) {
source share