ASP.NET 控制台应用程序 用记事本记录日志 实例

public static string path = Application.StartupPath;//获取执行文件的绝对地址

private static void WriteLog(string content)

        {
            //按日期自动生成txt记录日志
            //判断是否存在SYSLog文件夹
            if (Directory.Exists(@"" + path + "\\SYSLog") == false)
            {
                Directory.CreateDirectory(@"" + path + "\\SYSLog");
            }
            //按日期生成日志(先判断该文件是否存在)
            string txtName = DateTime.Now.ToString("yyyyMMdd");
            //if (File.Exists(@"" + path + "\\SYSLog\\" + txtName + ".txt") == false)
            //{
            //    //File.Create(@"" + path + "\\SYSLog\\" + txtName + ".txt");
            //    FileStream newfs= new FileStream(@"" + path + "\\SYSLog\\" + txtName + ".txt", FileMode.CreateNew);
            //}
            FileStream fs = new FileStream(path + "\\SYSLog\\" + txtName + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            StringBuilder sb = new StringBuilder();
            sb.Append("MessageService: ");
            sb.Append(content);
            sb.Append(" " + DateTime.Now.ToString() + "\n");
            m_streamWriter.WriteLine(sb.ToString());
            m_streamWriter.Flush();
            m_streamWriter.Close();
            fs.Close();
        }

猜你喜欢

转载自blog.csdn.net/qiufengwuxiu/article/details/79891944
今日推荐