方法一:以日期为日志文件名.

01.StreamWrite

  1. public void WriteLog(string msg)
  2. {
  3. string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
  4. if (!Directory.Exists(filePath))
  5. {
  6. Directory.CreateDirectory(filePath);
  7. }
  8. string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  9. try
  10. {
  11. using (StreamWriter sw = File.AppendText(logPath))
  12. {
  13. sw.WriteLine("消息:" + msg);
  14. sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  15. sw.WriteLine("**************************************************");
  16. sw.WriteLine();
  17. sw.Flush();
  18. sw.Close();
  19. sw.Dispose();
  20. }
  21. }
  22. catch (IOException e)
  23. {
  24. using (StreamWriter sw = File.AppendText(logPath))
  25. {
  26. sw.WriteLine("异常:" + e.Message);
  27. sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
  28. sw.WriteLine("**************************************************");
  29. sw.WriteLine();
  30. sw.Flush();
  31. sw.Close();
  32. sw.Dispose();
  33. }
  34. }
  35. }

02.泛型

  1. /// <summary>
  2. /// 打印日志
  3. /// </summary>
  4. /// <param name="msg"></param>
  5. public static void WriteLog(string msg)
  6. {
  7. string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
  8. if (!Directory.Exists(filePath))
  9. {
  10. Directory.CreateDirectory(filePath);
  11. }
  12. string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  13. try
  14. {
  15. using (StreamWriter sw=File.AppendText(logPath))
  16. {
  17. sw.WriteLine("消息:" + msg);
  18. sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  19. sw.WriteLine("**************************************************");
  20. sw.WriteLine();
  21. sw.Flush();
  22. sw.Close();
  23. sw.Dispose();
  24. }
  25. }
  26. catch (IOException e)
  27. {
  28. using (StreamWriter sw = File.AppendText(logPath))
  29. {
  30. sw.WriteLine("异常:" + e.Message);
  31. sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
  32. sw.WriteLine("**************************************************");
  33. sw.WriteLine();
  34. sw.Flush();
  35. sw.Close();
  36. sw.Dispose();
  37. }
  38. }
  39. }
  40. /// <summary>
  41. /// 打印Model日志
  42. /// </summary>
  43. /// <typeparam name="T"></typeparam>
  44. /// <param name="model"></param>
  45. public static void WriteLog<T>(T model) where T:class
  46. {
  47. string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
  48. if (!Directory.Exists(filePath))
  49. {
  50. Directory.CreateDirectory(filePath);
  51. }
  52. string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  53. try
  54. {
  55. using (StreamWriter sw = File.AppendText(logPath))
  56. {
  57. Type type = typeof(T);
  58. string msg = string.Join("*****", type.GetProperties().Select(m=>m.Name+":"+m.GetValue(model)));
  59. sw.WriteLine("消息:" + msg);
  60. sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  61. sw.WriteLine("**************************************************");
  62. sw.WriteLine();
  63. sw.Flush();
  64. sw.Close();
  65. sw.Dispose();
  66. }
  67. }
  68. catch (IOException e)
  69. {
  70. using (StreamWriter sw = File.AppendText(logPath))
  71. {
  72. sw.WriteLine("异常:" + e.Message);
  73. sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
  74. sw.WriteLine("**************************************************");
  75. sw.WriteLine();
  76. sw.Flush();
  77. sw.Close();
  78. sw.Dispose();
  79. }
  80. }
  81. }

方法二:以文档名称和日期结合作为文件名

  1. public void WriteLog(string documentName, string msg)
  2. {
  3. string errorLogFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log");
  4. if (!System.IO.Directory.Exists(errorLogFilePath))
  5. {
  6. System.IO.Directory.CreateDirectory(errorLogFilePath);
  7. }
  8. string logFile = System.IO.Path.Combine(errorLogFilePath, documentName + "@" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt");
  9. bool writeBaseInfo = System.IO.File.Exists(logFile);
  10. StreamWriter swLogFile = new StreamWriter(logFile, true, Encoding.Unicode);
  11. swLogFile.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "\t" + msg);
  12. swLogFile.Close();
  13. swLogFile.Dispose();
  14. }