LogBLL.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace DllFileSoc
  6. {
  7. public class LogBLL
  8. {
  9. //public static readonly object objLock = new object();
  10. // Path.DirectorySeparatorChar: '\'
  11. // Path.AltDirectorySeparatorChar: '/'正斜杠 (/) 字符。 它是 Unix 系统上唯一可识别的目录分隔符
  12. public static void Err(string msg, Exception ex)
  13. {
  14. string s = "err:" + msg + ":" + ex.Message;
  15. //string filepathname = AppDomain.CurrentDomain.BaseDirectory + "socketlog";
  16. string filepathname = DllPubInfo.PubInfo.LogPath;
  17. string filename = filepathname + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  18. if (!Directory.Exists(filepathname))//验证路径是否存在
  19. {
  20. Directory.CreateDirectory(filepathname);
  21. //不存在则创建
  22. }
  23. FileStream fs;
  24. StreamWriter sw;
  25. if (File.Exists(filename))
  26. //验证文件是否存在,有则追加,无则创建
  27. {
  28. fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
  29. }
  30. else
  31. {
  32. fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
  33. }
  34. sw = new StreamWriter(fs);
  35. //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") );
  36. sw.WriteLine(s);
  37. sw.WriteLine("time:" + DateTime.Now.ToString());
  38. sw.WriteLine("----------------------------------");
  39. sw.Close();
  40. fs.Close();
  41. }
  42. public static void Err(string msg)
  43. {
  44. try
  45. {
  46. string s = "Msg:" + msg;
  47. //string filepathname = AppDomain.CurrentDomain.BaseDirectory + "socketlog";
  48. string filepathname = DllPubInfo.PubInfo.LogPath;
  49. string filename = filepathname + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  50. if (!Directory.Exists(filepathname))//验证路径是否存在
  51. {
  52. Directory.CreateDirectory(filepathname);
  53. //不存在则创建
  54. }
  55. FileStream fs;
  56. StreamWriter sw;
  57. if (File.Exists(filename))
  58. //验证文件是否存在,有则追加,无则创建
  59. {
  60. fs = new FileStream(filename, FileMode.Append, FileAccess.Write,FileShare.ReadWrite);
  61. }
  62. else
  63. {
  64. fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
  65. }
  66. sw = new StreamWriter(fs);
  67. //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") );
  68. sw.WriteLine(s);
  69. sw.WriteLine("time:" + DateTime.Now.ToString());
  70. sw.WriteLine("----------------------------------");
  71. sw.Close();
  72. fs.Close();
  73. }
  74. catch
  75. {
  76. }
  77. }
  78. }
  79. }