Thứ Sáu, 26 tháng 9, 2014

Ghi log trong C#

 public void Log(string Message, TextWriter w)
    {
        w.Write("\r\nLog Entry: ");
        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
        w.WriteLine(" :");
        w.WriteLine(" :{0}", Message);
        w.WriteLine("-------------------------------");
    }
    public void DumpLog(StreamReader r)
    {
        string line;
        while ((line = r.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }



            using (StreamWriter w = File.AppendText(SererLogFileMapPath + "log.txt"))
            {
                Log(ex.Message, w);
            }
            using (StreamReader tr = File.OpenText(SererLogFileMapPath + "log.txt"))
            {
                DumpLog(tr);
            }

Không có nhận xét nào: