开发小技巧1——Logger

开发小技巧1——Logger
  在项目中加入静态Logger类,用于捕获并记录程序的进度、错误信息:
 
public static class Logger
    {
        public static void Log(string filePath, string msg)
        {
            using (FileStream fp = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                fp.Seek(fp.Length, SeekOrigin.Begin);
                using (StreamWriter writer = new StreamWriter(fp))
                {
                    writer.WriteLine(msg);
                }
            }
        }
    }

  需要用到的地方:

public void MyMethod()
        {
            try
            {
                //your method
            }
            catch (Exception exp)
            {
               Logger.Log("log.txt", exp.Message);
            }
        }

 

posted @ 2014-05-28 18:04  Infly  阅读(200)  评论(0编辑  收藏  举报