开发小技巧1——Logger

时间:2022-05-12 00:02:21
开发小技巧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);
}
}