Thursday, November 30, 2006

Writing log message to file via Message Queue



using System;
using System.Configuration;
using System.Messaging;
using System.IO;

namespace Test
{
public class QSend
{
private System.Messaging.MessageQueue mq;
private string exQ;

public QSend()
{
try
{
mq = new System.Messaging.MessageQueue(
@".\Private$\MyQ");
LogItems inLogItems = new LogItems();
inLogItems.FileName = @"d:\mgslog\Log.txt";
inLogItems.Message = " my message";
mq.Send(inLogItems);
}
catch(MessageQueueException ex)
{
exQ = ex.Message;
}
}
}

public class QRecieve
{
private System.Messaging.MessageQueue mq;
private string exQ;

public QRecieve()
{
try
{
mq = new System.Messaging.MessageQueue (@".\Private$\MyQ");
mq.Formatter =
new System.Messaging.XmlMessageFormatter (
new Type[] {typeof (LogItems)}) ;
System.Messaging.Message msg = mq.Receive();
LogItems outLogItems = (LogItems) msg.Body;
using( StreamWriter sw =
new StreamWriter(outLogItems.FileName,true))
{
sw.WriteLine(DateTime.Now.ToString()
+ outLogItems.Message);
sw.Flush();
}
}
catch(MessageQueueException e)
{
exQ = e.Message;
}

}
}

public class LogItems
{
private string _fileName;
private string _message;
public string FileName
{
get {return _fileName;}
set {_fileName = value;}
}
public string Message
{
get {return _message;}
set {_message = value;}
}
}
}




kick it on DotNetKicks.com

1 comment:

Anonymous said...

yugiohhentai yugiohhentai yugiohhentai yugiohhentai

This a good link's !
Posted by Admin