我想用c#编写一个日志文件。每当有消息发送到远程mq时。示例:假设将abcd消息发送到远程MQ,然后日志文件将记录类似的消息,在10-09-2018 11:30 AM在远程MQ中收到时间和日期。我怎么能做到这一点。可以编写这种代码吗?任何线索,任何想法,任何链接都会有所帮助。请帮忙。
编辑
MQQueueManager queueManager;
MQMessage queueMessage;
MQGetMessageOptions queueGetMessageOptions;
MQQueue queue;
string QueueName;
string QueueManagerName;
string ChannelInfo;
string channelName;
string PortNumber;
string transportType;
string connectionName;
QueueManagerName = ConfigurationManager.AppSettings["QueueManager"];
QueueName = ConfigurationManager.AppSettings["Queuename"];
ChannelInfo = ConfigurationManager.AppSettings["ChannelInformation"];
PortNumber = ConfigurationManager.AppSettings["Port"];
char[] separator = { '/' };
string[] ChannelParams;
ChannelParams = ChannelInfo.Split(separator);
channelName = ConfigurationManager.AppSettings["Channel"];
transportType = ConfigurationManager.AppSettings["TransportType"];
connectionName = ConfigurationManager.AppSettings["ConnectionName"];
String strReturn = "";
try
{
queueManager = new MQQueueManager(QueueManagerName,
channelName, connectionName);
strReturn = "Connected Successfully";
queue = queueManager.AccessQueue(QueueName,
MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
queueMessage = new MQMessage();
queueMessage.Format = MQC.MQFMT_STRING;
queueGetMessageOptions = new MQGetMessageOptions();
queue.Get(queueMessage, queueGetMessageOptions);
strReturn = queueMessage.ReadString(queueMessage.MessageLength);
}
catch (MQException exp)
{
strReturn = "Exception: " + exp.Message;
}
string path1 = @"C:\documents\Example.txt";
System.IO.File.WriteAllText(path1, strReturn);
答案 0 :(得分:0)
这个问题与MQ无关。这只是一个基本的C#编程问题。您可以使用许多C#日志记录框架。
这里是2种登录C#的基本方法。创建自己的“记录器”类并将其放入其中。
public static void WriteLog(String logFileName, byte[] data)
{
FileStream fs = null;
DateTime currentDT = DateTime.Now;
String header = currentDT.ToString("yyyy/MM/dd HH:mm:ss.fff") + " ";
String LF = "\n";
try
{
fs = new FileStream(logFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
if (fs.CanWrite)
{
fs.Write(Encoding.Default.GetBytes(header), 0, header.Length);
fs.Write(data, 0, data.Length);
fs.Write(Encoding.Default.GetBytes(LF), 0, LF.Length);
}
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
try
{
if (fs != null)
fs.Close();
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
public static void WriteLog(String logFileName, String data)
{
FileStream fs = null;
StreamWriter sw = null;
DateTime currentDT = DateTime.Now;
String header = currentDT.ToString("yyyy/MM/dd HH:mm:ss.fff") + " ";
String LF = "\n";
try
{
fs = new FileStream(logFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
if (fs.CanWrite)
{
sw = new StreamWriter(fs);
sw.Write(header);
sw.Write(data);
sw.Write(LF);
}
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
try
{
if (sw != null)
sw.Close();
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
try
{
if (fs != null)
fs.Close();
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
然后在MQ应用程序中,从队列成功获取消息后,您可以执行以下操作来记录该消息:
Logger.WriteLog("C:\temp\mylog.txt", "message received");