using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; using NLog; using NLog.Config; using OTS.WinFormsUI.Docking; using OTSModelSharp; namespace OTSMeasureApp { public struct PostLogMsg { public int logLevel;//1 trace 2 debug 3info 4 warn 5 error 6 fatal [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)2000)] public char[] logMessage; }; public partial class OTSMeasureOutputNlog : DockContent { //OTSCommon.Language lan; //Hashtable table; protected static NLog.Logger loger; private PostLogMsg m_LogMsg; public const int MsgID = 0x0464; public const int LogMsgID = 0x0465; public OTSMeasureOutputNlog() { InitializeComponent(); } private void OTSMeasureOutputNlog_Load(object sender, EventArgs e) { combox_NlogType.Items.Add("Trace"); combox_NlogType.Items.Add("Debug"); combox_NlogType.Items.Add("Info"); combox_NlogType.Items.Add("Warn"); combox_NlogType.Items.Add("Error"); combox_NlogType.Items.Add("Fatal"); combox_NlogType.SelectedIndex = 0; loger = NLog.LogManager.GetCurrentClassLogger(); //国际 //string aa=this.Name; //lan = new OTSCommon.Language(this); //table = lan.GetNameTable(this.Name); //button_Clear.Text = table["button_clear"].ToString(); } private void button_Test_Click(object sender, EventArgs e) { loger.Trace("Trace"); loger.Debug("Debug"); loger.Info("Info"); loger.Warn("Warn"); loger.Error("Error"); loger.Fatal("Fatal THIS IS A LOG WHICH USED TO TEST THE LOG LENTH FIT TO THE FORM!"); } private void button_Clear_Click(object sender, EventArgs e) { richTextBox_Nlog.Clear(); } private void combox_NlogType_SelectedIndexChanged(object sender, EventArgs e) { if (null == combox_NlogType.SelectedItem) return; String str = combox_NlogType.SelectedItem.ToString(); // 获取日志级别. LogLevel lv = LogLevel.Info; // 若选择的值无效, 则当作 Info级. lv = LogLevel.FromString(str); LoggingRule lr = LogManager.Configuration.LoggingRules.FirstOrDefault( r => r.Targets.Any( t => "control" == t.Name ) ); if (lr != null) { lr.SetLoggingLevels(lv, LogLevel.Fatal); LogManager.ReconfigExistingLoggers(); // 使配置生效. } } private void button_stop_Click(object sender, EventArgs e) { if (button_stop.Text == "Stop") { LoggingRule lr = LogManager.Configuration.LoggingRules.FirstOrDefault( r => r.Targets.Any( t => "control" == t.Name ) ); LogManager.Configuration.LoggingRules.Remove(lr); LogManager.ReconfigExistingLoggers(); button_stop.Text = "Start"; } else { if (null == combox_NlogType.SelectedItem) return; String str = combox_NlogType.SelectedItem.ToString(); LogManager.Configuration.AddRule(LogLevel.FromString(str), LogLevel.Fatal, "control"); LogManager.ReconfigExistingLoggers(); // 使配置生效. button_stop.Text = "Stop"; } } protected override void DefWndProc(ref Message m) { switch (m.Msg) { case MsgID: //MSTMsg = new STMrsSampleRetThreadMsg(); //MSTMsg.STMThreadStu.cMsrStartTime = new char[(int)MEMORY_SIZE.TIME_SIZE]; //MSTMsg.STMThreadStu.cMsrEndTime = new char[(int)MEMORY_SIZE.TIME_SIZE]; //MSTMsg.STMSampleRetData.BSEData.FieldPos = new Point(); //MSTMsg.STMSampleRetData.SFieldata.FieldPos = new Point(); //MSTMsg.STMSampleRetData.StartMsrField.FieldPos = new Point(); //MSTMsg.STMSampleStu.cMsrSName = new char[(int)MEMORY_SIZE.SAMPLE_NAME_SIZE]; //MSTMsg = (STMrsSampleRetThreadMsg)Marshal.PtrToStructure(m.LParam, typeof(STMrsSampleRetThreadMsg)); ////以下为实时接收图片数据,因为图片传出来的只是指针,所以必须立即接收,否则指针指向的数据就会被析构。 //Byte[] bBSEData; //int iHeight, iWidth; //iHeight = MSTMsg.STMSampleRetData.BSEData.iBSEDataHeight; //iWidth = MSTMsg.STMSampleRetData.BSEData.iBSEDataWidth; //if (iHeight > 0 && iWidth > 0) //{ // bBSEData = new Byte[iHeight * iWidth]; // Marshal.Copy(MSTMsg.STMSampleRetData.BSEData.pData, bBSEData, 0, iHeight * iWidth); // MSTMsg.gbseData = bBSEData; //} ////申请 //shareRes.mutex.WaitOne(); //m_MSTMsg.Add(MSTMsg); // 释放 //shareRes.mutex.ReleaseMutex(); break; case LogMsgID: m_LogMsg = new PostLogMsg(); m_LogMsg = (PostLogMsg)Marshal.PtrToStructure(m.LParam, typeof(PostLogMsg)); var log = NLog.LogManager.GetCurrentClassLogger(); string s = GetString(m_LogMsg.logMessage); switch (m_LogMsg.logLevel) { case 1: log.Trace(s); break; case 2: log.Debug(s); break; case 3: log.Info(s); break; case 4: log.Warn(s); break; case 5: log.Error(s); break; case 6: log.Fatal(s); break; } break; default: base.DefWndProc(ref m); break; } } private string GetString(char[] csStr) { int ilen = csStr.Length; string csName = new string(csStr); //MSTMsg.STMSampleStu.cSName csName.IndexOf('\0'); csName = csName.Substring(0, csName.IndexOf('\0')); return csName; } } }