Esempio n. 1
0
        /// <summary>
        /// 撰寫LOG方法
        /// </summary>
        /// <param name="LogFilePath">傳入LOG紀錄的位址</param>
        /// <param name="LogItemLevel">Log等級</param>
        /// <param name="ModuleName">模組名稱</param>
        /// <param name="UrlStr">URL</param>
        /// <param name="ErrorMessage">錯誤訊息</param>
        /// <param name="UserID">使用者</param>
        /// <param name="RequestFormStruct">表單狀態</param>
        /// <param name="SQLCommand">SQL命令</param>
        public void WriteLog(string LogFilePath,
                             LogLevel LogItemLevel,
                             string ModuleName,
                             string UrlStr,                             
                             string ErrorMessage,
                             string UserID,
                             RequestStruct[] RequestFormStruct,
                             string SQLCommand
                             )
        {
            StringBuilder sb = new StringBuilder();

            string LogFileName = string.Format("{0}{1}{2}.xml",
                                               DateTime.Now.Year.ToString(),
                                               DateTime.Now.Month.ToString().PadLeft(2, '0'),
                                               DateTime.Now.Day.ToString().PadLeft(2, '0')
                                               );
            string LogFilePathAndName = string.Format("{0}\\{1}",LogFilePath,LogFileName);



            //不存在檔案先建立
            if (!File.Exists(LogFilePathAndName))
            {
                StreamWriter sw = new StreamWriter(LogFilePathAndName,
                                                   true);
                sb.Append("<xml>\n");
                sb.Append("</xml>\n");

                sw.Write(sb.ToString());

                sw.Flush();
                sw.Close();
            }
            
            XmlDocument xdoc = new XmlDocument();
            //載入XML
            xdoc.Load(LogFilePathAndName);
            //選取node
            XmlNode RootNode = xdoc.SelectSingleNode("xml");

            //加入root element
            XmlElement LogRootElement = xdoc.CreateElement("log");

            //加入child element

            #region Module XMLElement

            //Module
            XmlElement ModuleElement = xdoc.CreateElement("Module");
            ModuleElement.InnerText = ModuleName;
            LogRootElement.AppendChild(ModuleElement);

            #endregion

            #region URL XMLElement

            //URL
            XmlElement URLElement = xdoc.CreateElement("URL");
            URLElement.InnerText = UrlStr;
            LogRootElement.AppendChild(URLElement);

            #endregion

            #region LogLevel XMLElement

            //LogLevel
            XmlElement LogLevelElement = xdoc.CreateElement("LogLevel");
            switch (LogItemLevel)
            {
                case LogLevel.None:
                                    LogLevelElement.InnerText = "None";
                                    break;
                case LogLevel.Error:
                                    LogLevelElement.InnerText = "Error";
                                    break;
                case LogLevel.Waring:
                                     LogLevelElement.InnerText = "Waring";
                                     break;
                case LogLevel.Paging:
                                     LogLevelElement.InnerText = "Paging";
                                     break;
                case LogLevel.Notice:
                                     LogLevelElement.InnerText = "Notice";
                                     break;
                case LogLevel.Information:
                                          LogLevelElement.InnerText = "Information";
                                          break;
                default:                            
                        break;
            }                
            LogRootElement.AppendChild(LogLevelElement);

            #endregion

            #region ErrorMessage XMLElement

            //ErrorMessage
            XmlElement ErrorMessageElement = xdoc.CreateElement("ErrorMessage");
            ErrorMessageElement.InnerText = ErrorMessage;
            LogRootElement.AppendChild(ErrorMessageElement);

            #endregion

            #region ControlState XMLElement


            //ControlState
            XmlElement ControlStateElement = xdoc.CreateElement("ControlState");
            if (RequestFormStruct != null)
            {
                for (int i = 0; i < RequestFormStruct.Length; i++)
                {
                    XmlElement ControlItemElement = xdoc.CreateElement("ControlItem");

                    //ControlID
                    XmlElement ControlIDElement = xdoc.CreateElement("ControlID");
                    ControlIDElement.InnerText = RequestFormStruct[i].ControlID;
                    ControlItemElement.AppendChild(ControlIDElement);

                    //Value
                    XmlElement ValueElement = xdoc.CreateElement("Value");
                    ValueElement.InnerText = RequestFormStruct[i].Value;
                    ControlItemElement.AppendChild(ValueElement);

                    //加入到 ControlState
                    ControlStateElement.AppendChild(ControlItemElement);
                }
            }
            else
            {
                XmlElement ControlItemElement = xdoc.CreateElement("ControlItem");
                
                XmlElement ControlIDElement = xdoc.CreateElement("ControlID");
                ControlItemElement.AppendChild(ControlIDElement);
                XmlElement ValueElement = xdoc.CreateElement("Value");
                ControlItemElement.AppendChild(ValueElement);

                //加入到 ControlState
                ControlStateElement.AppendChild(ControlItemElement);
            }

            LogRootElement.AppendChild(ControlStateElement);
           
            #endregion

            #region SQLCommand XMLElement

            //SQLCommand
            XmlElement SQLCommandElement = xdoc.CreateElement("SQLCommand");
            SQLCommandElement.InnerText = SQLCommand;
            LogRootElement.AppendChild(SQLCommandElement);

            #endregion

            #region UserID XMLElement

            //UserID
            XmlElement UserIDElement = xdoc.CreateElement("UserID");
            UserIDElement.InnerText = UserID;
            LogRootElement.AppendChild(UserIDElement);

            #endregion

            #region TimeElement

            //Time
            XmlElement TimeElement = xdoc.CreateElement("Time");
            TimeElement.InnerText = DateTime.Now.ToString();
            LogRootElement.AppendChild(TimeElement);

            #endregion
            
            //加到RootNode
            RootNode.AppendChild(LogRootElement);

            xdoc.Save(LogFilePathAndName);

        }
Esempio n. 2
0
        /// <summary>
        /// 寫入Information Log
        /// </summary>
        private void InformationLogProcess()
        {
            string URLStr = Request.ServerVariables["URL"].ToString();
            string[] WebSite = URLStr.Split('/');//位置2為小模組目錄名稱

            //Config存放路徑
            //string ConfigFilePath = Server.MapPath("/" + WebSite[1] + "/Config/");
            //string LogFilePath = Server.MapPath("/" + WebSite[1] + "/LOG");

            XmlDocument xdoc = new XmlDocument();            
            string LogSettingFilePathAndName = string.Format("{0}/{1}",
                                                             Application["Config_Path"].ToString(),
                                                             LogSettingFile
                                                             );

            xdoc.Load(LogSettingFilePathAndName);

            XmlNodeList RootNodeList = xdoc.SelectNodes("ProcessFile/add");



            for (int i = 0; i < RootNodeList.Count; i++)
            {
                string ProcessFile = RootNodeList[i].Attributes["File"].Value;
                SYSModel.LogLevel LogLevelProc = (SYSModel.LogLevel)int.Parse(RootNodeList[i].Attributes["LogLevel"].Value);                

                //如果所有程式或特定程式的Log等級設為Information
                if ( (( ProcessFile== "*") && (LogLevelProc == SYSModel.LogLevel.Information))  ||
                     (( ProcessFile== this.PageCode+".aspx") && (LogLevelProc == SYSModel.LogLevel.Information))
                    )

                {
                    SYSModel.LogHelper ProcLog = new SYSModel.LogHelper(ConnectionDB);
                    
                    SYSModel.RequestStruct[] Rqs = new SYSModel.RequestStruct[Request.Form.Count];

                    StringBuilder sb = new StringBuilder();

                    sb.Remove(0, sb.Length);

                    for (int j=0;j<Rqs.Length;j++)
                    {
                        //Rqs[j].ControlID = Request.Form.GetKey(j);
                        //Rqs[j].Value = Request.Form.GetValues(j)[0].ToString(); ;
                        sb.AppendFormat("控制項名稱:{0},內容{1};",
                                        Request.Form.GetKey(j).ToString(),
                                        Request.Form.GetValues(j)[0].ToString()
                                        );
                    }

                    ParameterList.Clear();

                    //Server名稱
                    ParameterList.Add(Request.ServerVariables["Server_Name"]);
                    //模組名稱
                    ParameterList.Add(WebSite[2]);
                    //URL
                    ParameterList.Add(Request.Path);
                    //LOGLEVEL
                    ParameterList.Add(SYSModel.LogLevel.Information);
                    //錯誤訊息
                    ParameterList.Add("紀錄Information");
                    //控制項內容
                    ParameterList.Add(sb.ToString());
                    //SQL語法
                    ParameterList.Add("");
                    try
                    {
                        //使用者
                        ParameterList.Add(Session["UID"].ToString());
                    }
                    catch (Exception ex)
                    {
                        //使用者
                        ParameterList.Add("SessionUID Timeout");
                    }

                    PerformanceCounter PC = new PerformanceCounter();
                    PerformanceCounter PC1 = new PerformanceCounter();
                    try
                    {
                        //目前記憶體總量
                        PC.CategoryName = PerformanceCounter_TotalMemoryCategoryName;
                        PC.CounterName = PerformanceCounter_TotalMemoryCounterName;
                        PC.InstanceName = PerformanceCounter_TotalMemoryInstanceName;                                                                       
                        PC.ReadOnly = true;                                                                       

                        ParameterList.Add(PC.NextValue());
                        
                    }
                    catch (Exception ex)
                    {
                        ParameterList.Add(-1);
                    }
                    finally
                    {
                        try
                        {
                            //未釋放的記憶體
                            PC1.CategoryName = PerformanceCounter_HeapMemoryCategoryName;
                            PC1.CounterName = PerformanceCounter_HeapMemoryCounterName;
                            PC1.InstanceName = PerformanceCounter_HeapMemoryInstanceName;
                            PC1.ReadOnly = true;

                            ParameterList.Add(PC1.NextValue());
                        }
                        catch (Exception ex)
                        {
                            ParameterList.Add(-1);
                        }
                        finally
                        {
                            //檢查變數項目是否足夠
                            ParameterListIndexCheck(WebSite[2],
                                                    SYSModel.LogLevel.Information,
                                                    sb.ToString()
                                                    );

                            //寫入DB
                            ProcLog.WriteLog(ParameterList,
                                             null
                                             );

                            PC.Close();

                            PC1.Close();
                        }

                    }

                }
            }
            
            
        }