LogLevelSwitchTests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Text.Json;
  2. using Serilog.Events;
  3. using YZWater.Core.Models;
  4. using YZWater.Core.Services;
  5. namespace YZWater.Core.Tests.Services;
  6. /// <summary>
  7. /// 日志级别运行时开关的回归测试。
  8. /// 背景:轮询诊断降到 Debug 之后,如果级别不可调,现场遇到"某个值不对"就没有任何手段
  9. /// 把它临时开回来取证,只能停下来重新发布 —— 降级就变成了丢能力。
  10. /// </summary>
  11. // 本类会改 LogService.MinimumLevel(静态),必须与同样改它的测试类串行,否则默认值断言偶发失败
  12. [Collection("Serilog minimum level")]
  13. public class LogLevelSwitchTests : IDisposable
  14. {
  15. private readonly LogEventLevel _original = LogService.MinimumLevel;
  16. [Fact]
  17. public void Default_MinimumLevel_IsInformation()
  18. {
  19. Assert.Equal(LogEventLevel.Information, _original);
  20. }
  21. [Fact]
  22. public void ApplyMinimumLevel_Debug_TakesEffect()
  23. {
  24. LogService.ApplyMinimumLevel("Debug");
  25. Assert.Equal(LogEventLevel.Debug, LogService.MinimumLevel);
  26. }
  27. [Fact]
  28. public void ApplyMinimumLevel_IsCaseInsensitive()
  29. {
  30. LogService.ApplyMinimumLevel("warning");
  31. Assert.Equal(LogEventLevel.Warning, LogService.MinimumLevel);
  32. }
  33. [Theory]
  34. [InlineData(null)]
  35. [InlineData("")]
  36. [InlineData(" ")]
  37. [InlineData("banana")]
  38. [InlineData("Infos")]
  39. public void ApplyMinimumLevel_InvalidValue_KeepsCurrentLevel(string? level)
  40. {
  41. LogService.ApplyMinimumLevel("Debug");
  42. var before = LogService.MinimumLevel;
  43. LogService.ApplyMinimumLevel(level);
  44. Assert.Equal(before, LogService.MinimumLevel);
  45. }
  46. [Theory]
  47. [InlineData("Debug", true)]
  48. [InlineData("debug", true)]
  49. [InlineData("Information", true)]
  50. [InlineData("Verbose", true)]
  51. [InlineData("Fatal", true)]
  52. [InlineData("Trace", false)]
  53. // 手改配置文件容易多敲空格,Enum.TryParse 本身容忍首尾空白 —— 这里把它钉住
  54. [InlineData("Debug ", true)]
  55. [InlineData(" Debug", true)]
  56. public void TryParseLevel_MatchesSerilogVocabulary(string level, bool expected)
  57. {
  58. Assert.Equal(expected, LogService.TryParseLevel(level, out _));
  59. }
  60. [Fact]
  61. public void SystemConfig_LogKnobs_DefaultToProductionValues()
  62. {
  63. var config = new SystemConfig();
  64. Assert.Equal("Information", config.MinimumLogLevel);
  65. Assert.Equal(60, config.LogSnapshotIntervalSeconds);
  66. }
  67. [Fact]
  68. public void SystemConfig_LogKnobs_RoundTripThroughJson()
  69. {
  70. // 现场排障就是改这两行文本,键名必须能被反序列化认出来
  71. const string json = """{"MinimumLogLevel":"Debug","LogSnapshotIntervalSeconds":10}""";
  72. var config = JsonSerializer.Deserialize<SystemConfig>(json);
  73. Assert.Equal("Debug", config?.MinimumLogLevel);
  74. Assert.Equal(10, config?.LogSnapshotIntervalSeconds);
  75. }
  76. [Fact]
  77. public void PlcPollingService_SnapshotInterval_DefaultsTo60Seconds()
  78. {
  79. // 0 会让快照静默关闭,默认值必须是有证据的那一档
  80. Assert.Equal(60, PlcPollingService.Instance.LogSnapshotIntervalSeconds);
  81. }
  82. public void Dispose() => LogService.ApplyMinimumLevel(_original.ToString());
  83. }