LogWriter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. using System.IO;
  11. using Utilities;
  12. namespace SMBServer
  13. {
  14. public class LogWriter
  15. {
  16. private string m_logsDirectoryPath;
  17. private object m_syncLock = new object();
  18. private FileStream m_logFile;
  19. private DateTime? m_logFileDate;
  20. public LogWriter()
  21. {
  22. string assemblyDirectory = GetAssemblyDirectory();
  23. m_logsDirectoryPath = assemblyDirectory + @"Logs\";
  24. }
  25. public LogWriter(string logsDirectoryPath)
  26. {
  27. m_logsDirectoryPath = logsDirectoryPath;
  28. }
  29. /// <exception cref="System.IO.IOException"></exception>
  30. /// <exception cref="System.UnauthorizedAccessException"></exception>
  31. private void OpenLogFile()
  32. {
  33. if (m_logFileDate.HasValue && m_logFileDate.Value != DateTime.Today && m_logFile != null)
  34. {
  35. m_logFile.Close();
  36. m_logFile = null;
  37. m_logFileDate = null;
  38. }
  39. if (m_logFileDate == null)
  40. {
  41. m_logFileDate = DateTime.Today;
  42. string logFilePath = String.Format("{0}{1}-{2}-{3}.log", m_logsDirectoryPath, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"));
  43. try
  44. {
  45. if (!Directory.Exists(m_logsDirectoryPath))
  46. {
  47. Directory.CreateDirectory(m_logsDirectoryPath);
  48. }
  49. m_logFile = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.Read, 0x1000, FileOptions.WriteThrough);
  50. }
  51. catch
  52. {
  53. }
  54. }
  55. }
  56. public void CloseLogFile()
  57. {
  58. if (m_logFile != null)
  59. {
  60. lock (m_syncLock)
  61. {
  62. m_logFile.Close();
  63. m_logFile = null;
  64. }
  65. }
  66. }
  67. public void WriteLine(string value, params object[] args)
  68. {
  69. WriteLine(String.Format(value, args));
  70. }
  71. public void WriteLine(string value)
  72. {
  73. lock (m_syncLock)
  74. {
  75. OpenLogFile();
  76. if (m_logFile != null)
  77. {
  78. StreamWriter writer = new StreamWriter(m_logFile);
  79. writer.WriteLine(value);
  80. writer.Flush();
  81. }
  82. }
  83. }
  84. public void OnLogEntryAdded(object sender, LogEntry entry)
  85. {
  86. if (entry.Severity != Severity.Trace)
  87. {
  88. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  89. WriteLine("{0} {1} [{2}] {3}", entry.Severity.ToString().PadRight(12), timestamp, entry.Source, entry.Message);
  90. }
  91. }
  92. public static string GetAssemblyDirectory()
  93. {
  94. Assembly assembly = Assembly.GetEntryAssembly();
  95. if (assembly == null)
  96. {
  97. assembly = Assembly.GetExecutingAssembly();
  98. }
  99. string assemblyDirectory = Path.GetDirectoryName(assembly.Location);
  100. if (!assemblyDirectory.EndsWith(@"\"))
  101. {
  102. assemblyDirectory += @"\";
  103. }
  104. return assemblyDirectory;
  105. }
  106. }
  107. }