OTSMeasureOutputNlog.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. using NLog;
  8. using NLog.Config;
  9. using OTS.WinFormsUI.Docking;
  10. using OTSModelSharp;
  11. namespace OTSMeasureApp
  12. {
  13. public struct PostLogMsg
  14. {
  15. public int logLevel;//1 trace 2 debug 3info 4 warn 5 error 6 fatal
  16. [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)2000)]
  17. public char[] logMessage;
  18. };
  19. public partial class OTSMeasureOutputNlog : DockContent
  20. {
  21. //OTSCommon.Language lan;
  22. //Hashtable table;
  23. protected static NLog.Logger loger;
  24. private PostLogMsg m_LogMsg;
  25. public const int MsgID = 0x0464;
  26. public const int LogMsgID = 0x0465;
  27. public OTSMeasureOutputNlog()
  28. {
  29. InitializeComponent();
  30. }
  31. private void OTSMeasureOutputNlog_Load(object sender, EventArgs e)
  32. {
  33. combox_NlogType.Items.Add("Trace");
  34. combox_NlogType.Items.Add("Debug");
  35. combox_NlogType.Items.Add("Info");
  36. combox_NlogType.Items.Add("Warn");
  37. combox_NlogType.Items.Add("Error");
  38. combox_NlogType.Items.Add("Fatal");
  39. combox_NlogType.SelectedIndex = 0;
  40. loger = NLog.LogManager.GetCurrentClassLogger();
  41. //国际
  42. //string aa=this.Name;
  43. //lan = new OTSCommon.Language(this);
  44. //table = lan.GetNameTable(this.Name);
  45. //button_Clear.Text = table["button_clear"].ToString();
  46. }
  47. private void button_Test_Click(object sender, EventArgs e)
  48. {
  49. loger.Trace("Trace");
  50. loger.Debug("Debug");
  51. loger.Info("Info");
  52. loger.Warn("Warn");
  53. loger.Error("Error");
  54. loger.Fatal("Fatal THIS IS A LOG WHICH USED TO TEST THE LOG LENTH FIT TO THE FORM!");
  55. }
  56. private void button_Clear_Click(object sender, EventArgs e)
  57. {
  58. richTextBox_Nlog.Clear();
  59. }
  60. private void combox_NlogType_SelectedIndexChanged(object sender, EventArgs e)
  61. {
  62. if (null == combox_NlogType.SelectedItem) return;
  63. String str = combox_NlogType.SelectedItem.ToString(); // 获取日志级别.
  64. LogLevel lv = LogLevel.Info; // 若选择的值无效, 则当作 Info级.
  65. lv = LogLevel.FromString(str);
  66. LoggingRule lr = LogManager.Configuration.LoggingRules.FirstOrDefault(
  67. r => r.Targets.Any(
  68. t => "control" == t.Name
  69. )
  70. );
  71. if (lr != null)
  72. {
  73. lr.SetLoggingLevels(lv, LogLevel.Fatal);
  74. LogManager.ReconfigExistingLoggers(); // 使配置生效.
  75. }
  76. }
  77. private void button_stop_Click(object sender, EventArgs e)
  78. {
  79. if (button_stop.Text == "Stop")
  80. {
  81. LoggingRule lr = LogManager.Configuration.LoggingRules.FirstOrDefault(
  82. r => r.Targets.Any(
  83. t => "control" == t.Name
  84. )
  85. );
  86. LogManager.Configuration.LoggingRules.Remove(lr);
  87. LogManager.ReconfigExistingLoggers();
  88. button_stop.Text = "Start";
  89. }
  90. else
  91. {
  92. if (null == combox_NlogType.SelectedItem) return;
  93. String str = combox_NlogType.SelectedItem.ToString();
  94. LogManager.Configuration.AddRule(LogLevel.FromString(str), LogLevel.Fatal, "control");
  95. LogManager.ReconfigExistingLoggers(); // 使配置生效.
  96. button_stop.Text = "Stop";
  97. }
  98. }
  99. protected override void DefWndProc(ref Message m)
  100. {
  101. switch (m.Msg)
  102. {
  103. case MsgID:
  104. //MSTMsg = new STMrsSampleRetThreadMsg();
  105. //MSTMsg.STMThreadStu.cMsrStartTime = new char[(int)MEMORY_SIZE.TIME_SIZE];
  106. //MSTMsg.STMThreadStu.cMsrEndTime = new char[(int)MEMORY_SIZE.TIME_SIZE];
  107. //MSTMsg.STMSampleRetData.BSEData.FieldPos = new Point();
  108. //MSTMsg.STMSampleRetData.SFieldata.FieldPos = new Point();
  109. //MSTMsg.STMSampleRetData.StartMsrField.FieldPos = new Point();
  110. //MSTMsg.STMSampleStu.cMsrSName = new char[(int)MEMORY_SIZE.SAMPLE_NAME_SIZE];
  111. //MSTMsg = (STMrsSampleRetThreadMsg)Marshal.PtrToStructure(m.LParam, typeof(STMrsSampleRetThreadMsg));
  112. ////以下为实时接收图片数据,因为图片传出来的只是指针,所以必须立即接收,否则指针指向的数据就会被析构。
  113. //Byte[] bBSEData;
  114. //int iHeight, iWidth;
  115. //iHeight = MSTMsg.STMSampleRetData.BSEData.iBSEDataHeight;
  116. //iWidth = MSTMsg.STMSampleRetData.BSEData.iBSEDataWidth;
  117. //if (iHeight > 0 && iWidth > 0)
  118. //{
  119. // bBSEData = new Byte[iHeight * iWidth];
  120. // Marshal.Copy(MSTMsg.STMSampleRetData.BSEData.pData, bBSEData, 0, iHeight * iWidth);
  121. // MSTMsg.gbseData = bBSEData;
  122. //}
  123. ////申请
  124. //shareRes.mutex.WaitOne();
  125. //m_MSTMsg.Add(MSTMsg);
  126. // 释放
  127. //shareRes.mutex.ReleaseMutex();
  128. break;
  129. case LogMsgID:
  130. m_LogMsg = new PostLogMsg();
  131. m_LogMsg = (PostLogMsg)Marshal.PtrToStructure(m.LParam, typeof(PostLogMsg));
  132. var log = NLog.LogManager.GetCurrentClassLogger();
  133. string s = GetString(m_LogMsg.logMessage);
  134. switch (m_LogMsg.logLevel)
  135. {
  136. case 1:
  137. log.Trace(s);
  138. break;
  139. case 2:
  140. log.Debug(s);
  141. break;
  142. case 3:
  143. log.Info(s);
  144. break;
  145. case 4:
  146. log.Warn(s);
  147. break;
  148. case 5:
  149. log.Error(s);
  150. break;
  151. case 6:
  152. log.Fatal(s);
  153. break;
  154. }
  155. break;
  156. default:
  157. base.DefWndProc(ref m);
  158. break;
  159. }
  160. }
  161. private string GetString(char[] csStr)
  162. {
  163. int ilen = csStr.Length;
  164. string csName = new string(csStr); //MSTMsg.STMSampleStu.cSName
  165. csName.IndexOf('\0');
  166. csName = csName.Substring(0, csName.IndexOf('\0'));
  167. return csName;
  168. }
  169. }
  170. }