LogBLL.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace DllFileSoc
  6. {
  7. class LogBLL
  8. {
  9. //public static readonly object objLock = new object();
  10. public static void Err(string msg, Exception ex)
  11. {
  12. string s = "err:" + msg + ":" + ex.Message;
  13. string filepathname = AppDomain.CurrentDomain.BaseDirectory + "socketlog";
  14. string filename = filepathname + "\\" + DateTime.Now.ToString("yyyy-MM-dd HH") + ".txt";
  15. if (!Directory.Exists(filepathname))//验证路径是否存在
  16. {
  17. Directory.CreateDirectory(filepathname);
  18. //不存在则创建
  19. }
  20. FileStream fs;
  21. StreamWriter sw;
  22. if (File.Exists(filename))
  23. //验证文件是否存在,有则追加,无则创建
  24. {
  25. fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
  26. }
  27. else
  28. {
  29. fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
  30. }
  31. sw = new StreamWriter(fs);
  32. //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") );
  33. sw.WriteLine(s);
  34. sw.WriteLine("time:" + DateTime.Now.ToString());
  35. sw.WriteLine("----------------------------------");
  36. sw.Close();
  37. fs.Close();
  38. }
  39. public static void Err(string msg)
  40. {
  41. string s = "Msg:" + msg ;
  42. string filepathname = AppDomain.CurrentDomain.BaseDirectory + "socketlog";
  43. string filename = filepathname + "\\" + DateTime.Now.ToString("yyyy-MM-dd HH") + ".txt";
  44. if (!Directory.Exists(filepathname))//验证路径是否存在
  45. {
  46. Directory.CreateDirectory(filepathname);
  47. //不存在则创建
  48. }
  49. FileStream fs;
  50. StreamWriter sw;
  51. if (File.Exists(filename))
  52. //验证文件是否存在,有则追加,无则创建
  53. {
  54. fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
  55. }
  56. else
  57. {
  58. fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
  59. }
  60. sw = new StreamWriter(fs);
  61. //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") );
  62. sw.WriteLine(s);
  63. sw.WriteLine("time:" + DateTime.Now.ToString());
  64. sw.WriteLine("----------------------------------");
  65. sw.Close();
  66. fs.Close();
  67. }
  68. }
  69. }