WebLog.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace FastReport.Web
  5. {
  6. internal class WebLog
  7. {
  8. private object locker = new object();
  9. private StringBuilder log = new StringBuilder();
  10. private bool showStackTrace = false;
  11. private string logFileName;
  12. public string Text
  13. {
  14. get { return log.ToString(); }
  15. }
  16. public string LogFile
  17. {
  18. get { return logFileName; }
  19. set { logFileName = value; }
  20. }
  21. public void Add(string line)
  22. {
  23. log.Append(line).Append("<br />");
  24. }
  25. public void Clear()
  26. {
  27. log = new StringBuilder();
  28. }
  29. public void AddError(Exception e)
  30. {
  31. lock (locker)
  32. {
  33. Add(String.Format("<span style=\"color:red\"><b>ERROR:</b><br /> {0}</span>", e.Message.Replace("\n", "<br />")));
  34. if (showStackTrace)
  35. Add(e.StackTrace);
  36. }
  37. }
  38. public void Flush()
  39. {
  40. log.AppendLine().AppendLine();
  41. if (!String.IsNullOrEmpty(logFileName))
  42. {
  43. try
  44. {
  45. lock (locker)
  46. {
  47. if (File.Exists(logFileName))
  48. {
  49. using (FileStream file = new FileStream(logFileName, FileMode.Append))
  50. using (StreamWriter writer = new StreamWriter(file, Encoding.UTF8))
  51. {
  52. writer.Write(log);
  53. }
  54. }
  55. else
  56. {
  57. using (FileStream file = new FileStream(logFileName, FileMode.Create))
  58. using (StreamWriter writer = new StreamWriter(file, Encoding.UTF8))
  59. {
  60. writer.Write(log);
  61. }
  62. }
  63. }
  64. }
  65. catch
  66. {
  67. //
  68. }
  69. }
  70. }
  71. public WebLog(bool trace)
  72. {
  73. showStackTrace = trace;
  74. }
  75. }
  76. }