分布式日志系统
在微服务架构中,应用程序被拆分为多个小型的、自治的服务,日志分散在各个服务实例中。当系统出现问题时,需要登录到不同的服务器、查看不同的日志文件,排查效率极低。因此,建立统一的分布式日志系统成为微服务架构中的必备基础设施。
HMX 平台采用 Grafana + Loki + Promtail 的轻量级日志聚合方案,集中采集 .NET 应用程序日志、第三方组件日志以及基础设施日志,实现日志的统一存储、查询与可视化分析。
mermaid
%%{init: {'theme': 'base', 'themeVariables': {'fontSize': '14px'}}}%%
graph LR
%% 定义节点样式
classDef logSource fill:#eaeaff,stroke:#c4c4e6,stroke-width:1px
classDef agent fill:#eaeaff,stroke:#c4c4e6,stroke-width:1px
classDef storage fill:#eaeaff,stroke:#c4c4e6,stroke-width:1px
classDef visual fill:#eaeaff,stroke:#c4c4e6,stroke-width:1px
%% 定义子图容器样式
classDef layerBox fill:#fcfae0,stroke:#d8d4a8,stroke-width:1px
%% --- 1. 日志源层 ---
subgraph Layer1 ["日志源"]
direction TB
S1[".NET 10 应用<br/>NLog 日志"]:::logSource
S2["帆软报表<br/>业务日志"]:::logSource
S3["RustFS<br/>对象存储日志"]:::logSource
S4["基础设施<br/>系统/容器日志"]:::logSource
end
%% --- 2. 日志采集层 ---
subgraph Layer2 ["日志采集层"]
direction TB
P1["Promtail"]:::agent
P2["Promtail"]:::agent
P3["Promtail"]:::agent
P4["Promtail"]:::agent
end
%% --- 3. 存储与查询层 ---
subgraph Layer3 ["存储与查询层"]
Loki["Loki<br/>日志存储引擎"]:::storage
end
%% --- 4. 可视化层 ---
subgraph Layer4 ["可视化层"]
Grafana["Grafana<br/>日志查询与仪表板"]:::visual
end
%% --- 建立连接关系 ---
S1 -- 推送 --> P1
S2 -- 推送 --> P2
S3 -- 推送 --> P3
S4 -- 推送 --> P4
P1 -- 汇聚 --> Loki
P2 -- 汇聚 --> Loki
P3 -- 汇聚 --> Loki
P4 -- 汇聚 --> Loki
Loki -- 查询 --> Grafana
%% 应用大框背景样式
class Layer1,Layer2,Layer3,Layer4 layerBox1. 应用日志采集(NLog)
平台集成 NLog 作为 .NET 10 应用程序的日志记录框架,日志输出格式化为 JSON,便于 Promtail 采集和 Loki 索引。
2. 日志采集器(Promtail)
Promtail 负责采集各类日志文件并推送到 Loki,支持多来源配置。
3. 日志存储与查询(Loki)
Loki 采用轻量级设计,仅索引日志元数据(标签),不索引日志全文,大幅降低存储成本。
4. 可视化分析(Grafana)
通过 Grafana 的 Explore 功能进行日志查询和分析。
日志全链路追踪示例
当用户发起一个跨服务的请求(如生成报表)时,通过 TraceId 串联完整的日志链路:

设计优势
| 能力维度 | HMX 日志方案优势 |
|---|---|
| 轻量高效 | Loki 无需 Elasticsearch 级别的资源消耗,适合中小规模部署 |
| 统一查询 | 在 Grafana 中同时查看应用、报表、存储、系统等多源日志 |
| 成本可控 | 日志内容不索引,存储成本低,支持分层存储策略 |
| 原生集成 | Grafana 原生支持 Loki,无需额外开发即可完成可视化 |
| 扩展友好 | 新增服务只需配置 Promtail 采集路径,自动纳入日志系统 |
