예제 #1
0
        public List <LoggerEntity> GetLoggerList()
        {
            List <LoggerEntity> list = new List <LoggerEntity>();
            XDocument           doc  = XDocument.Load(configFilePath);
            var loggers = doc.Descendants("logger").ToList();

            if (loggers != null)
            {
                foreach (var logger in loggers)
                {
                    LoggerEntity l = new LoggerEntity();
                    l.loggerName = logger.Attribute("name").Value;

                    var appenders = logger.Elements("appender-ref").ToList();
                    if (appenders != null)
                    {
                        foreach (var appender in appenders)
                        {
                            for (var i = 0; i < traceAppenders.Length; i++)
                            {
                                if (appender.Attribute("ref").Value.Contains(traceAppenders[i]))
                                {
                                    l.traceList[traceAppenders[i]] = true;
                                    continue;
                                }
                            }
                        }
                    }

                    list.Add(l);
                }
            }

            return(list);
        }
예제 #2
0
        //先删除,都添加即为更新
        public bool updateLogger(LoggerEntity logger)
        {
            try
            {
                RemoveLogger(logger.loggerName);
                AddLogger(logger);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }
예제 #3
0
        public List <XElement> MakeAppnderElements(LoggerEntity logger)
        {
            List <XElement> ppnderElements = new List <XElement>();

            foreach (var ap in logger.traceList)
            {
                if (ap.Value == true)
                {
                    //数据库目前只用一个appender,不用创建了
                    if (ap.Key == "AdoNetAppender_sqlserver")
                    {
                        continue;
                    }

                    var savePath = logger.loggerName + "\\" + ap.Key + "_";
                    if (logger.saveDir != string.Empty)
                    {
                        savePath = logger.saveDir + "\\" + savePath;
                    }

                    //每个appender 以名字加key结尾
                    XElement apenderElement = new XElement("appender", new XAttribute("name", logger.loggerName + "_" + ap.Key),
                                                           new XAttribute("type", "log4net.Appender.RollingFileAppender"),
                                                           new XElement("param", new XAttribute("name", "File"), new XAttribute("value", savePath)),
                                                           new XElement("param", new XAttribute("name", "AppendToFile"), new XAttribute("value", "true")),
                                                           new XElement("param", new XAttribute("name", "DatePattern"), new XAttribute("value", "yyyy-MM-dd\".log\"")),
                                                           new XElement("param", new XAttribute("name", "RollingStyle"), new XAttribute("value", "Date")),
                                                           new XElement("param", new XAttribute("name", "StaticLogFileName"), new XAttribute("value", "false")),
                                                           new XElement("layout", new XAttribute("type", "log4net.Layout.PatternLayout"),
                                                                        new XElement("param", new XAttribute("name", "ConversionPattern"), new XAttribute("value", "%d [%t] %-5p %x %m %n"))
                                                                        ),
                                                           new XElement("filter", new XAttribute("type", "log4net.Filter.LevelRangeFilter"),
                                                                        new XElement("param", new XAttribute("name", "LevelMin"), new XAttribute("value", ap.Key)),
                                                                        new XElement("param", new XAttribute("name", "LevelMax"), new XAttribute("value", ap.Key))
                                                                        ));

                    ppnderElements.Add(apenderElement);
                }
            }
            return(ppnderElements);
        }
예제 #4
0
        public XElement MakeLoggerElement(LoggerEntity logger)
        {
            XElement newlogger = new XElement("logger", new XAttribute("name", logger.loggerName));

            foreach (var ap in logger.traceList)
            {
                if (ap.Value == true)
                {
                    //数据库目前只用一个appender
                    if (ap.Key == "AdoNetAppender_sqlserver")
                    {
                        newlogger.Add(new XElement("appender-ref", new XAttribute("ref", "AdoNetAppender_sqlserver")));
                        continue;
                    }

                    //每个appender 以名字加key结尾
                    newlogger.Add(new XElement("appender-ref", new XAttribute("ref", logger.loggerName + "_" + ap.Key)));
                }
            }
            return(newlogger);
        }
예제 #5
0
        private void loggerView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1)
            {
                return;
            }

            if (this.txtConfigPath.Text.Trim() == "")
            {
                MessageBox.Show("请选择log4net配置文件的路径!");
                return;
            }

            if (this.txtLoggerFilePath.Text.Trim() == "")
            {
                MessageBox.Show("请选择logger.cs文件的路径!");
                return;
            }
            var chkMsg = ConfigHandler.CheckConfig(this.txtConfigPath.Text.Trim(), this.txtLoggerFilePath.Text.Trim());

            if (chkMsg != "success")
            {
                MessageBox.Show(chkMsg);
                return;
            }


            string        action = loggerView.Columns[e.ColumnIndex].Name;//操作类型
            ConfigHandler config = new ConfigHandler(this.txtConfigPath.Text, this.txtLoggerFilePath.Text);

            switch (action)
            {
            case "update":
                LoggerEntity logger = new LoggerEntity();
                logger.loggerName = loggerView.Rows[e.RowIndex].Cells[0].Value.ToString();

                logger.traceList["INFO"]  = loggerView.Rows[e.RowIndex].Cells[1].Value.ToString() == "True";
                logger.traceList["DEBUG"] = loggerView.Rows[e.RowIndex].Cells[2].Value.ToString() == "True";
                logger.traceList["WARN"]  = loggerView.Rows[e.RowIndex].Cells[3].Value.ToString() == "True";
                logger.traceList["ERROR"] = loggerView.Rows[e.RowIndex].Cells[4].Value.ToString() == "True";
                logger.traceList["FATAL"] = loggerView.Rows[e.RowIndex].Cells[5].Value.ToString() == "True";
                logger.traceList["AdoNetAppender_sqlserver"] = loggerView.Rows[e.RowIndex].Cells[6].Value.ToString() == "True";
                logger.sqlConnstring = string.Empty;      //连接串之前添加时已经添加了,所以这里不用再设置了。
                logger.saveDir       = txtSaveDir.Text.Trim();

                logger.sqlConnstring = txtDbstring2.Text.Trim();
                if (logger.traceList["AdoNetAppender_sqlserver"] && logger.sqlConnstring == string.Empty || logger.sqlConnstring.Contains("数据库名"))
                {
                    MessageBox.Show("请填写正确的数据库连接字段串!数据库名,账号,密码请填对");
                    groupdbstring.Visible = true;
                    return;
                }
                groupdbstring.Visible = false;


                if (logger.isAllTraceSetFalse())
                {
                    MessageBox.Show("至少选择一个Appnder!!!");
                    return;
                }


                try
                {
                    if (config.updateLogger(logger))
                    {
                        bindLoggerGridView();
                        setConfigXml();

                        MessageBox.Show("OK,更新logger成功!");
                    }
                    else
                    {
                        MessageBox.Show("Err----OH!,更新logger失败!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("SORRY" + ex.ToString());
                }


                break;

            case "delete":
                if (MessageBox.Show("确定删除吗?", "删除提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    var loggerName = loggerView.Rows[e.RowIndex].Cells[0].Value.ToString();
                    try
                    {
                        config.RemoveLogger(loggerName);
                        bindLoggerGridView();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Sorry  ,删除失败了" + ex.ToString());
                    }
                }
                break;

            default:
                break;
            }
        }
예제 #6
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.txtConfigPath.Text.Trim() == "")
            {
                MessageBox.Show("请选择log4net配置文件的路径!");
                return;
            }

            if (this.txtLoggerFilePath.Text.Trim() == "")
            {
                MessageBox.Show("请选择logger.cs文件的路径!");
                return;
            }

            var chkMsg = ConfigHandler.CheckConfig(this.txtConfigPath.Text.Trim(), this.txtLoggerFilePath.Text.Trim());

            if (chkMsg != "success")
            {
                MessageBox.Show(chkMsg);
                return;
            }

            if (this.txtLoggerName.Text.Trim() == "")
            {
                MessageBox.Show("填写logger名称!");
                return;
            }

            LoggerEntity logger = new LoggerEntity();

            logger.loggerName = this.txtLoggerName.Text;

            logger.traceList["INFO"]  = chk_supportInfo.Checked;
            logger.traceList["DEBUG"] = chk_supportDebug.Checked;
            logger.traceList["WARN"]  = chk_supportWarn.Checked;
            logger.traceList["ERROR"] = chk_supportError.Checked;
            logger.traceList["FATAL"] = chk_supportFatal.Checked;
            logger.traceList["AdoNetAppender_sqlserver"] = chk_supportDb.Checked;
            logger.sqlConnstring = txtDBstring.Text.Trim();
            logger.saveDir       = txtSaveDir.Text.Trim();



            if (logger.traceList["AdoNetAppender_sqlserver"] && logger.sqlConnstring == string.Empty || logger.sqlConnstring.Contains("数据库名"))
            {
                MessageBox.Show("请填写正确的数据库连接字段串!数据库名,账号,密码请填对");
                return;
            }

            if (logger.isAllTraceSetFalse())
            {
                MessageBox.Show("至少选择一个Appnder!!!");
                return;
            }

            ConfigHandler config = new ConfigHandler(this.txtConfigPath.Text, this.txtLoggerFilePath.Text);

            if (config.IsExistLogger(logger.loggerName))
            {
                lblTip.Text = "已经存在此logger名称:" + txtLoggerName.Text;
                return;
            }
            else
            {
                lblTip.Text = "";
            }


            try
            {
                if (config.AddLogger(logger))
                {
                    bindLoggerGridView();

                    setConfigXml();
                    MessageBox.Show("OK,创建logger成功!");
                }
                else
                {
                    MessageBox.Show("Err----OH!,创建logger失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("SORRY" + ex.ToString());
            }
        }
예제 #7
0
        public bool AddLogger(LoggerEntity logger)
        {
            if (logger.loggerName == "")
            {
                throw new Exception(logger.loggerName + ":logger 名称不充许为空!");
            }

            if (logger.isAllTraceSetFalse())
            {
                throw new Exception(logger.loggerName + "至少选择一个Appnder!!!");
            }

            if (IsExistLogger(logger.loggerName))
            {
                throw new  Exception(logger.loggerName + ":此logger 已经存在");
            }

            try{
                XDocument doc       = XDocument.Load(configFilePath);
                XElement  newLogger = MakeLoggerElement(logger);

                if (doc.Descendants("logger").ToList().Count == 0)
                {
                    doc.Descendants("root").Last().AddAfterSelf(newLogger);
                }
                else
                {
                    doc.Descendants("logger").Last().AddAfterSelf(newLogger);
                }



                List <XElement> appenders = MakeAppnderElements(logger);
                foreach (var ap in appenders)
                {
                    doc.Descendants("appender").Last().AddBeforeSelf(ap);
                }


                //sql数据库字符串
                if (logger.traceList["AdoNetAppender_sqlserver"] && logger.sqlConnstring != string.Empty)
                {
                    var aps = doc.Descendants("appender").ToList();
                    if (aps != null)
                    {
                        foreach (var ap in aps)
                        {
                            if (ap.Attribute("name").Value == "AdoNetAppender_sqlserver")
                            {
                                ap.Descendants("connectionString").ToList()[0].Attribute("value").Value = logger.sqlConnstring;
                            }
                        }
                    }
                }

                //保存配置文件
                doc.Save(configFilePath);

                //重新logger.cs
                ResetLoggerFile();
            }
            catch (Exception e) {
                throw e;
            }

            return(true);
        }