如题

解决方案 »

  1.   

    log4net
      

  2.   

    记录日志,无非就是创建一个日志文件。ublic class CreateLogFiles
    {    string sLogFormat = "";
        string sErrorTime = "";
        TStagingApp_Log_SQL _tstagingapplog = null;
        public CreateLogFiles()
        {
            try
            {
                sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";            sErrorTime=System.DateTime.Now.ToString("yyyyMMdd");
            }
            catch (Exception ex)
            {
                _tstagingapplog = new TStagingApp_Log_SQL();
                _tstagingapplog.CreatedOn = System.DateTime.Now;
                _tstagingapplog.Notes = ex.Message;
                _tstagingapplog.Save();
     
            }
        }    public void ErrorLog(string sPathName, string sErrMsg)
        {
            string path = sPathName + sErrorTime + ".txt";
            string ServiceName = HttpContext.Current.Server.MachineName;
            string IpAddress = HttpContext.Current.Request.UserHostAddress;
            if (!File.Exists(path))
            {
                try
                {
                   
                    using (StreamWriter sw = File.CreateText(path))
                    {
                       
                        sw.WriteLine("ServiceName:" + ServiceName + "\r\n" + "IPAddress:" + IpAddress + "\r\n" + sLogFormat + "\r\n" + sErrMsg);
                        sw.Flush();
                        sw.Close();
                    }
                    //StreamWriter sw = new StreamWriter(sPathName + sErrorTime + ".txt", true, System.Text.Encoding.Unicode);//when the property set false,then file data is bestrow.            }
                catch (Exception ex)
                {
                    _tstagingapplog = new TStagingApp_Log_SQL();
                    _tstagingapplog.CreatedOn = System.DateTime.Now;
                    _tstagingapplog.Notes = ex.Message;
                    _tstagingapplog.Save();
                }
            }
            else
            {
                try
                {
                    using (StreamWriter sw = File.AppendText(path))
                    {
                        
                        if ((File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                            File.SetAttributes(path, FileAttributes.Normal);
                        sw.WriteLine("ServiceName:" + ServiceName + "\r\n" + "IPAddress:" + IpAddress + "\r\n" + sLogFormat +"\r\n"+ sErrMsg);
                        sw.Flush();
                        sw.Close();
                    }                //StreamWriter sw = new StreamWriter(sPathName + sErrorTime + ".txt", true, System.Text.Encoding.Unicode);//when the property set false,then file data is bestrow.            }
                catch (Exception ex)
                {
                    _tstagingapplog = new TStagingApp_Log_SQL();
                    _tstagingapplog.CreatedOn = System.DateTime.Now;
                    _tstagingapplog.Notes = ex.Message;
                    _tstagingapplog.Save();
                }
            }
        }
    }