PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/App_Code/Logs.cs

https://bitbucket.org/xpertech/paycell
C# | 61 lines | 50 code | 11 blank | 0 comment | 5 complexity | 3a9e2e836eeaa05af72d3c786d1960f5 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Configuration;
  7. public class Logs
  8. {
  9. public static void Write(string Msg)
  10. {
  11. try
  12. {
  13. System.Web.UI.Page page = new System.Web.UI.Page();
  14. string FileName =page.Server.MapPath("App_Data/Logs.log");
  15. string NewFileName =page.Server.MapPath("App_Data/LogsBak.log");
  16. int MaxLogsLines = 1000;
  17. int NoOfLine = 0;
  18. if (File.Exists(FileName))
  19. {
  20. StreamReader sr = new StreamReader(FileName);
  21. while (sr.ReadLine() != null)
  22. {
  23. NoOfLine = NoOfLine + 1;
  24. }
  25. sr.Close();
  26. }
  27. StreamWriter sw = new StreamWriter(FileName, true);
  28. if (NoOfLine < Convert.ToInt16(MaxLogsLines))
  29. {
  30. sw.WriteLine(DateTime.Now.ToString() + "--" + Msg);
  31. sw.Flush();
  32. sw.Close();
  33. }
  34. else
  35. {
  36. sw.WriteLine(DateTime.Now.ToString() + "--" + Msg);
  37. sw.Flush();
  38. sw.Close();
  39. if (File.Exists(NewFileName))
  40. {
  41. File.Delete(NewFileName);
  42. }
  43. File.Move(FileName,NewFileName);
  44. }
  45. }
  46. catch (Exception)
  47. {
  48. }
  49. }
  50. }