Ibm mq 如何在将消息发送到远程mq时写入日志文件

Ibm mq 如何在将消息发送到远程mq时写入日志文件,ibm-mq,Ibm Mq,我想用c#写一个日志文件。每次消息到达远程mq时。示例:假设abcd消息发送到远程mq,则日志文件将记录在2018年9月10日上午11:30远程mq中接收到like 1消息,并输入时间和日期。我怎样才能做到这一点。有可能写这种代码吗?任何线索,任何想法,任何链接都会有帮助。请帮忙 编辑 MQQueueManager queueManager; MQMessage queueMessage; MQGetMessageOptions queueGetM

我想用c#写一个日志文件。每次消息到达远程mq时。示例:假设abcd消息发送到远程mq,则日志文件将记录在2018年9月10日上午11:30远程mq中接收到like 1消息,并输入时间和日期。我怎样才能做到这一点。有可能写这种代码吗?任何线索,任何想法,任何链接都会有帮助。请帮忙

编辑

        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);

这个问题与MQ无关。这只是一个基本的C#编程问题。有很多C#的日志框架可以使用

下面是登录C#的两种基本方法。创建自己的“Logger”类并将它们放入其中

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");

到目前为止你试过什么?您写入日志文件的确切问题是什么?我可以连接到远程mq,连接后我可以将消息保存到文本文件中,但我希望在下载消息之前写入收到的消息