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)

            {

            }

        }

 

posted @ 2020-04-17 15:21  离。  阅读(42)  评论(0编辑  收藏