FileStreamLogFactory.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace CommonLang.Log
  7. {
  8. public class FileStreamLogFactory : LoggerFactory
  9. {
  10. private TextWriter writer;
  11. private FileStream output;
  12. public FileStreamLogFactory(FileStream output)
  13. {
  14. this.output = output;
  15. this.writer = new StreamWriter(output);
  16. }
  17. protected override Logger CreateLogger(string name)
  18. {
  19. FileStreamLogger log = new FileStreamLogger(writer);
  20. log.SetName(name);
  21. return log;
  22. }
  23. }
  24. public class FileStreamLogger : Logger
  25. {
  26. private TextWriter writer;
  27. public FileStreamLogger(TextWriter writer)
  28. {
  29. this.writer = writer;
  30. }
  31. protected override void Print(string text)
  32. {
  33. lock (writer)
  34. {
  35. writer.WriteLine(text);
  36. writer.Flush();
  37. }
  38. }
  39. }
  40. }