OperationLogOptions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Concurrent;
  3. namespace Aivfo.OperationLog
  4. {
  5. /// <summary>
  6. /// 组件配置(14§10 可配置)。集中持有,支持运行时改(热生效简化为:直接改属性即下次记日志生效)。
  7. /// 模块级开关:默认全开,可按模块名设 开/关 + 最低级别。
  8. /// </summary>
  9. public sealed class OperationLogOptions
  10. {
  11. /// <summary>端/服务名(project 字段),如 "operate" / "front"。</summary>
  12. public string Project { get; set; } = "operate";
  13. /// <summary>Kafka 地址,如 "localhost:9092"。</summary>
  14. public string KafkaBootstrapServers { get; set; } = "localhost:9092";
  15. /// <summary>Kafka topic。</summary>
  16. public string Topic { get; set; } = "tl-oplog";
  17. /// <summary>全局总开关。关掉则完全不记(连入队都跳过)。</summary>
  18. public bool Enabled { get; set; } = true;
  19. /// <summary>
  20. /// 全局最低级别。低于此级别的日志不发 Kafka。
  21. /// 默认 Info:只发操作级;调试级(Debug)默认走本地文件、不入 Kafka。
  22. /// </summary>
  23. public OpLogLevel GlobalLevel { get; set; } = OpLogLevel.Info;
  24. /// <summary>内存队列上限。满了走降级(丢弃 Debug / 落本地兜底)。</summary>
  25. public int QueueCapacity { get; set; } = 10000;
  26. /// <summary>后台发送批量大小。</summary>
  27. public int BatchSize { get; set; } = 100;
  28. /// <summary>本地文件目录(调试级 + 兜底)。</summary>
  29. public string LocalLogDir { get; set; } = "oplog_local";
  30. /// <summary>设备上下文:培养箱 SN(可选,作为全局默认)。</summary>
  31. public string TlSn { get; set; }
  32. /// <summary>本机 host(默认取机器名)。</summary>
  33. public string Host { get; set; } = SafeMachineName();
  34. // 模块级开关:moduleName -> (enabled, minLevel)。缺省即全开、按 GlobalLevel。
  35. private readonly ConcurrentDictionary<string, ModuleSetting> _modules =
  36. new ConcurrentDictionary<string, ModuleSetting>();
  37. /// <summary>设置某模块的开关与最低级别(运行时可调,热生效)。</summary>
  38. public void SetModule(string module, bool enabled, OpLogLevel minLevel = OpLogLevel.Info)
  39. {
  40. if (string.IsNullOrEmpty(module)) return;
  41. _modules[module] = new ModuleSetting { Enabled = enabled, MinLevel = minLevel };
  42. }
  43. /// <summary>判断某模块是否启用(总开关 + 模块开关)。关闭则连本地调试文件也不写。</summary>
  44. public bool IsModuleEnabled(string module)
  45. {
  46. if (!Enabled) return false;
  47. if (!string.IsNullOrEmpty(module) && _modules.TryGetValue(module, out var s))
  48. return s.Enabled;
  49. return true;
  50. }
  51. /// <summary>
  52. /// 判断某模块某级别是否应**发 Kafka 入库**(操作级门槛)。
  53. /// 调试级(Debug)天然低于 Info,故默认不发 Kafka、走本地文件——这是 14§4 两级设计。
  54. /// 要让某模块的调试级也入库(很少用),把该模块 MinLevel 设为 Debug。
  55. /// </summary>
  56. public bool ShouldSendKafka(string module, OpLogLevel level)
  57. {
  58. if (!IsModuleEnabled(module)) return false;
  59. if (!string.IsNullOrEmpty(module) && _modules.TryGetValue(module, out var s))
  60. return level >= s.MinLevel;
  61. return level >= GlobalLevel;
  62. }
  63. private static string SafeMachineName()
  64. {
  65. try { return Environment.MachineName; }
  66. catch { return "unknown"; }
  67. }
  68. private sealed class ModuleSetting
  69. {
  70. public bool Enabled;
  71. public OpLogLevel MinLevel;
  72. }
  73. }
  74. }