using NLog;
using NLog.Config;
using NLog.Targets;
//using OTS.WinFormsUI.Docking;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace OxfordTest
{
    public partial class OTSMeasureOutputNlog: Form
    {
        //OTSSysMgrTools.Language lan;
        //Hashtable table;
        protected static NLog.Logger log;
        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;
            log = NLog.LogManager.GetCurrentClassLogger();

            //国际
            //lan = new OTSSysMgrTools.Language(this);
            //table = lan.GetNameTable(this.Name);
            //label1.Text = table["label1"].ToString();
            //button_Clear.Text = table["button_clear"].ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            log.Trace("Trace");
            log.Debug("Debug");
            log.Info("Info");
            log.Warn("Warn");
            log.Error("Error");
            log.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";
            }
        }

      
    }
}
