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.

40 lines
1.6 KiB

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