ASP.NET把數據寫入.TXT文本文件裏

public static void CreateWebLog(string logStr)

        {
            try

            {
                string dir = System.Web.HttpContext.Current.Server.MapPath("~/log");

                if (Directory.Exists(dir) == false)

                {

                    Directory.CreateDirectory(dir);

                }

                string strFilePath = System.Web.HttpContext.Current.Server.MapPath("~/log/log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");

                FileInfo logFile = new FileInfo(strFilePath);

                System.IO.FileStream fs;

                if (logFile.Exists)

                {

                    fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);

                }

                else

                {

                    fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Create);

                }

                System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);

                sw.WriteLine("---------------------------------------------------------------------------------------");

                sw.WriteLine("-----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---------------------------------------");

                sw.WriteLine("---------------------------------------------------------------------------------------");

                sw.WriteLine(logStr);

                sw.Close();

                fs.Close();

            }

            catch (Exception)

            {

            }

        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章