You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using IPA.Logging;
  8. namespace IPA.Logging.Printers
  9. {
  10. /// <summary>
  11. /// A printer for all messages to a unified log location.
  12. /// </summary>
  13. public class GlobalLogFilePrinter : GZFilePrinter
  14. {
  15. /// <summary>
  16. /// Provides a filter for this specific printer.
  17. /// </summary>
  18. public override Logger.LogLevel Filter { get; set; } = Logger.LogLevel.All;
  19. /// <summary>
  20. /// Prints an entry to the associated file.
  21. /// </summary>
  22. /// <param name="level">the <see cref="Logger.Level"/> of the message</param>
  23. /// <param name="time">the <see cref="DateTime"/> the message was recorded at</param>
  24. /// <param name="logName">the name of the log that sent the message</param>
  25. /// <param name="message">the message to print</param>
  26. public override void Print(Logger.Level level, DateTime time, string logName, string message)
  27. {
  28. foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
  29. fileWriter.WriteLine(string.Format(Logger.LogFormat, line, logName, time, level.ToString().ToUpper()));
  30. }
  31. /// <summary>
  32. /// Gets the <see cref="FileInfo"/> for the target file.
  33. /// </summary>
  34. /// <returns></returns>
  35. protected override FileInfo GetFileInfo()
  36. {
  37. var logsDir = new DirectoryInfo("Logs");
  38. logsDir.Create();
  39. var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm}.log"));
  40. return finfo;
  41. }
  42. }
  43. }