Skip to content

Commit

Permalink
✨ feat(promtail-config.yaml): 新增 api-log 任務以過濾包含 api 的日誌
Browse files Browse the repository at this point in the history
✨ feat(Program.cs): 啟用 GrafanaLoki 日誌輸出功能並添加必要標籤
  • Loading branch information
yaochangyu committed Jan 20, 2025
1 parent d430ee6 commit bd0d13d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
14 changes: 7 additions & 7 deletions StructLog/Lab.Loki/Lab.Loki.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
.WriteTo.Console(formatter) // 使用 JSON 格式輸出
.WriteTo.File(formatter, "logs/aspnet-.txt", rollingInterval: RollingInterval.Hour) //正式環境不要用 File
.WriteTo.Seq("http://localhost:5341", payloadFormatter: formatter)
// .WriteTo.GrafanaLoki( "http://loki:3100",
// labels: new[] {
// new LokiLabel { Key = "service", Value = "api" }
// },
// credentials: null,
// propertiesAsLabels: new[] { "RequestId", "RequestPath", "StatusCode" }
// )
.WriteTo.GrafanaLoki( "http://loki:3100",
labels: new[] {
new LokiLabel { Key = "service", Value = "api" }
},
credentials: null,
propertiesAsLabels: new[] { "RequestId", "RequestPath", "StatusCode" }
)
.CreateBootstrapLogger();
Log.Information("Starting web host");

Expand Down
32 changes: 31 additions & 1 deletion StructLog/Lab.Loki/promtail/promtail-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,34 @@ scrape_configs:
format: RFC3339Nano
- output:
source: message
format: "Input line: {{ .line }}"
format: "Input line: {{ .line }}"

- job_name: api-log
docker_sd_configs:
- host: unix:///var/run/docker.sock
refresh_interval: 5s
relabel_configs:
- source_labels: ['__meta_docker_container_name']
target_label: 'container'
# 只保留 container name 包含 api 的 log
- source_labels: ['__meta_docker_container_name']
#regex: '/?*api*'
regex: '/?.*api.*'
#regex: '/?labloki.*'
action: keep
pipeline_stages:
- timestamp:
source: time
format: RFC3339Nano
- output:
source: message
format: "Input line: {{ .line }}"
- json:
expressions:
RequestId: RequestId
RequestPath: RequestPath
StatusCode: StatusCode
- labels:
RequestId:
RequestPath:
StatusCode:

0 comments on commit bd0d13d

Please sign in to comment.