The easy to use and full featured Irc Bot everyone is talking about!
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. namespace Combot
  9. {
  10. public static class Logger
  11. {
  12. public static void LogToFile(string directory, string fileName, string message, int maxSize)
  13. {
  14. if (!Directory.Exists(directory))
  15. Directory.CreateDirectory(directory);
  16. string logFile = Path.Combine(directory, fileName + Constants.LOGFILEEXT);
  17. // Check to see if we need to create a new log
  18. if (File.Exists(logFile))
  19. {
  20. TrimLogFile(directory, fileName, maxSize);
  21. }
  22. // Write the log to the main log file
  23. StreamWriter logWriter = File.AppendText(logFile);
  24. logWriter.WriteLine(string.Format("[{0}] {1}", DateTime.Now.ToString("G"), message));
  25. logWriter.Close();
  26. }
  27. private static void TrimLogFile(string logDir, string fileName, int maxSize)
  28. {
  29. string logFile = Path.Combine(logDir, fileName);
  30. FileInfo file = new FileInfo(logFile);
  31. long fileSize = file.Length;
  32. if (fileSize > maxSize)
  33. {
  34. // The file is too large, we need to increment the file names of the log files
  35. string[] files = Directory.GetFiles(logDir);
  36. for (int i = files.GetUpperBound(0) - 1; i >= 0; i--)
  37. {
  38. string newFileName = fileName + "_" + (i + 1) + Constants.LOGFILEEXT;
  39. string newFile = Path.Combine(logDir, newFileName);
  40. File.Move(files[i], newFile);
  41. }
  42. }
  43. }
  44. }
  45. }