| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 | 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;            //国际            //string aa=this.Name;            //lan = new OTSCommon.Language(this);            //table = lan.GetNameTable(this.Name);            //button_Clear.Text = table["button_clear"].ToString();            //this.BeginInvoke(new Action(() => {            //    this.Hide();            //    this.Opacity = 1;            //}));        }        private void button_Test_Click(object sender, EventArgs e)        {            loger = NLog.LogManager.GetCurrentClassLogger();            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;        }    }}
 |