OTSMeasureOutputNlog.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. //国际
  41. //string aa=this.Name;
  42. //lan = new OTSCommon.Language(this);
  43. //table = lan.GetNameTable(this.Name);
  44. //button_Clear.Text = table["button_clear"].ToString();
  45. //this.BeginInvoke(new Action(() => {
  46. // this.Hide();
  47. // this.Opacity = 1;
  48. //}));
  49. }
  50. private void button_Test_Click(object sender, EventArgs e)
  51. {
  52. loger = NLog.LogManager.GetCurrentClassLogger();
  53. loger.Trace("Trace");
  54. loger.Debug("Debug");
  55. loger.Info("Info");
  56. loger.Warn("Warn");
  57. loger.Error("Error");
  58. loger.Fatal("Fatal THIS IS A LOG WHICH USED TO TEST THE LOG LENTH FIT TO THE FORM!");
  59. }
  60. private void button_Clear_Click(object sender, EventArgs e)
  61. {
  62. richTextBox_Nlog.Clear();
  63. }
  64. private void combox_NlogType_SelectedIndexChanged(object sender, EventArgs e)
  65. {
  66. if (null == combox_NlogType.SelectedItem) return;
  67. String str = combox_NlogType.SelectedItem.ToString(); // 获取日志级别.
  68. LogLevel lv = LogLevel.Info; // 若选择的值无效, 则当作 Info级.
  69. lv = LogLevel.FromString(str);
  70. LoggingRule lr = LogManager.Configuration.LoggingRules.FirstOrDefault(
  71. r => r.Targets.Any(
  72. t => "control" == t.Name
  73. )
  74. );
  75. if (lr != null)
  76. {
  77. lr.SetLoggingLevels(lv, LogLevel.Fatal);
  78. LogManager.ReconfigExistingLoggers(); // 使配置生效.
  79. }
  80. }
  81. private void button_stop_Click(object sender, EventArgs e)
  82. {
  83. if (button_stop.Text == "Stop")
  84. {
  85. LoggingRule lr = LogManager.Configuration.LoggingRules.FirstOrDefault(
  86. r => r.Targets.Any(
  87. t => "control" == t.Name
  88. )
  89. );
  90. LogManager.Configuration.LoggingRules.Remove(lr);
  91. LogManager.ReconfigExistingLoggers();
  92. button_stop.Text = "Start";
  93. }
  94. else
  95. {
  96. if (null == combox_NlogType.SelectedItem) return;
  97. String str = combox_NlogType.SelectedItem.ToString();
  98. LogManager.Configuration.AddRule(LogLevel.FromString(str), LogLevel.Fatal, "control");
  99. LogManager.ReconfigExistingLoggers(); // 使配置生效.
  100. button_stop.Text = "Stop";
  101. }
  102. }
  103. protected override void DefWndProc(ref Message m)
  104. {
  105. switch (m.Msg)
  106. {
  107. case MsgID:
  108. //MSTMsg = new STMrsSampleRetThreadMsg();
  109. //MSTMsg.STMThreadStu.cMsrStartTime = new char[(int)MEMORY_SIZE.TIME_SIZE];
  110. //MSTMsg.STMThreadStu.cMsrEndTime = new char[(int)MEMORY_SIZE.TIME_SIZE];
  111. //MSTMsg.STMSampleRetData.BSEData.FieldPos = new Point();
  112. //MSTMsg.STMSampleRetData.SFieldata.FieldPos = new Point();
  113. //MSTMsg.STMSampleRetData.StartMsrField.FieldPos = new Point();
  114. //MSTMsg.STMSampleStu.cMsrSName = new char[(int)MEMORY_SIZE.SAMPLE_NAME_SIZE];
  115. //MSTMsg = (STMrsSampleRetThreadMsg)Marshal.PtrToStructure(m.LParam, typeof(STMrsSampleRetThreadMsg));
  116. ////以下为实时接收图片数据,因为图片传出来的只是指针,所以必须立即接收,否则指针指向的数据就会被析构。
  117. //Byte[] bBSEData;
  118. //int iHeight, iWidth;
  119. //iHeight = MSTMsg.STMSampleRetData.BSEData.iBSEDataHeight;
  120. //iWidth = MSTMsg.STMSampleRetData.BSEData.iBSEDataWidth;
  121. //if (iHeight > 0 && iWidth > 0)
  122. //{
  123. // bBSEData = new Byte[iHeight * iWidth];
  124. // Marshal.Copy(MSTMsg.STMSampleRetData.BSEData.pData, bBSEData, 0, iHeight * iWidth);
  125. // MSTMsg.gbseData = bBSEData;
  126. //}
  127. ////申请
  128. //shareRes.mutex.WaitOne();
  129. //m_MSTMsg.Add(MSTMsg);
  130. // 释放
  131. //shareRes.mutex.ReleaseMutex();
  132. break;
  133. case LogMsgID:
  134. m_LogMsg = new PostLogMsg();
  135. m_LogMsg = (PostLogMsg)Marshal.PtrToStructure(m.LParam, typeof(PostLogMsg));
  136. var log = NLog.LogManager.GetCurrentClassLogger();
  137. string s = GetString(m_LogMsg.logMessage);
  138. switch (m_LogMsg.logLevel)
  139. {
  140. case 1:
  141. log.Trace(s);
  142. break;
  143. case 2:
  144. log.Debug(s);
  145. break;
  146. case 3:
  147. log.Info(s);
  148. break;
  149. case 4:
  150. log.Warn(s);
  151. break;
  152. case 5:
  153. log.Error(s);
  154. break;
  155. case 6:
  156. log.Fatal(s);
  157. break;
  158. }
  159. break;
  160. default:
  161. base.DefWndProc(ref m);
  162. break;
  163. }
  164. }
  165. private string GetString(char[] csStr)
  166. {
  167. int ilen = csStr.Length;
  168. string csName = new string(csStr); //MSTMsg.STMSampleStu.cSName
  169. csName.IndexOf('\0');
  170. csName = csName.Substring(0, csName.IndexOf('\0'));
  171. return csName;
  172. }
  173. }
  174. }