当前位置:编程学习 > C#/ASP.NET >>

C# 写日志文件

 public static void WriteLog(string txt)

        {

            try

            {

                string path = Application.StartupPath + @"\log\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";

                if (!Directory.Exists(path))

                {

                    Directory.CreateDirectory(path);

                }

                path +=  DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("HH") + ".txt";

                if (!File.Exists(path))

                {

                    File.Create(path);

                }

                FileStream fs;

                StreamWriter sw;

                fs = new FileStream(path, FileMode.Append);

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

                sw.Write(DateTime.Now.ToString("HH:mm:ss") + " " + txt + "\r\n");

                sw.Close();

                fs.Close();

            }

            catch (Exception ex)

            {

                WriteLog("程序发生异常(WriteLog)。详情:" + ex.Message);

            }

        }

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,