Beispiel #1
0
        /// <summary>
        /// 保存日志:[email protected]
        /// </summary>
        /// <param Name="Content">日志内容</param>
        /// <param Name="PostContent">提交内容</param>
        /// <param Name="LogType">日志类型,1=登录日志,2=操作日志,3=错误日志</param>
        public void WriteLog(string Content, string PostContent, int LogType)
        {
            string ip         = Utils.GetIP();                                                                                                                  //IP地址
            string ScriptName = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"] + "?" + HttpContext.Current.Request.ServerVariables["QUERY_STRING"]; //提交的页面地址
            //string ScriptName = HttpContext.Current.Request.Url.ToString (); //提交的页面地址
            //int urlNum = ScriptName.IndexOf(HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"]);
            //if (urlNum > 0)
            //    ScriptName = ScriptName.Substring(urlNum);

            SiteParamConfig pci = SiteParam.GetConfig(GetSiteParamPath);

            if (pci.LogType == "0")  //保存到数据库
            {
                KingTop.Model.SysManage.SysLog objlog    = new KingTop.Model.SysManage.SysLog();
                KingTop.BLL.SysManage.SysLog   objBllLog = new KingTop.BLL.SysManage.SysLog();
                objlog.IP      = ip;
                objlog.Content = Content;
                objlog.LogType = LogType;

                // objlog.NodeId = NodeId;

                objlog.NodeCode = NodeCode;

                objlog.PostContent = PostContent;
                objlog.ScriptName  = ScriptName;
                objlog.SiteID      = SiteID;
                objlog.UserNo      = UserNo;
                objBllLog.Save("NEW", objlog);
            }
            else  //保存到文件
            {
                string       logPath  = pci.LogDir;
                StreamWriter sw       = null;
                DateTime     date     = DateTime.Now;
                string       FileName = date.Year + "-" + date.Month;
                if (logPath.IndexOf(":") == -1)     //相对路径
                {
                    if (logPath.Trim().Length == 0) //路径为空
                    {
                        logPath = "Logs";
                    }
                    logPath = HttpContext.Current.Server.MapPath("~/" + logPath);
                }

                try
                {
                    FileName = logPath + "/" + UserNo + "_" + FileName + "-" + DesSecurity.DesEncrypt(FileName, "hqbonline") + "-s.log";

                    #region 检测日志目录是否存在

                    if (!Directory.Exists(logPath))
                    {
                        Directory.CreateDirectory(logPath);
                    }

                    if (!File.Exists(FileName))
                    {
                        sw = File.CreateText(FileName);
                    }
                    else
                    {
                        sw = File.AppendText(FileName);
                    }
                    #endregion

                    sw.WriteLine("[NodeCode]       :" + NodeCode + "\r");
                    sw.WriteLine("[SiteID]       :" + SiteID + "\r");
                    sw.WriteLine("[Content]      :" + Content);
                    sw.WriteLine("[AddDate]      :" + date.ToString("yyyy-MM-dd hh:mm:ss") + "\r");
                    sw.WriteLine("[UserNo]       :" + UserNo + "\r");
                    sw.WriteLine("[IP]           :" + ip + "\r");
                    sw.WriteLine("[ScriptName]   :" + ScriptName + "\r");
                    sw.WriteLine("[PostContent]  :" + PostContent + "\r");
                    sw.WriteLine("[LogType]      :" + LogType + "\r");
                    sw.WriteLine("≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\r");
                    sw.Flush();
                }
                finally
                {
                    if (sw != null)
                    {
                        sw.Close();
                    }
                }
            }
        }