Skip to content

Commit 42dad42

Browse files
committed
[增加]1. 增加Loki的写入支持
1 parent 92a7c00 commit 42dad42

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

GameFrameX.Foundation.Logger/GameFrameX.Foundation.Logger.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
<ItemGroup>
5050
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
5151
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
52-
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
52+
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0"/>
53+
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="8.3.0"/>
5354
</ItemGroup>
5455

5556
<ItemGroup>

GameFrameX.Foundation.Logger/LogHandler.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using Serilog;
88
using Serilog.Events;
9+
using Serilog.Sinks.Grafana.Loki;
910

1011
namespace GameFrameX.Foundation.Logger;
1112

@@ -63,6 +64,32 @@ public static ILogger Create(LogOptions logOptions, bool isDefault = true, Actio
6364
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
6465
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
6566
.WriteTo.File(logPath, rollingInterval: logOptions.RollingInterval, rollOnFileSizeLimit: logOptions.IsFileSizeLimit, fileSizeLimitBytes: logOptions.FileSizeLimitBytes, retainedFileCountLimit: logOptions.RetainedFileCountLimit);
67+
if (logOptions.IsGrafanaLoki)
68+
{
69+
var grafanaLokiLabels = new List<LokiLabel>();
70+
foreach (var kv in logOptions.GrafanaLokiLabels)
71+
{
72+
var lokiLabel = new LokiLabel
73+
{
74+
Key = kv.Key,
75+
Value = kv.Value,
76+
};
77+
grafanaLokiLabels.Add(lokiLabel);
78+
}
79+
80+
LokiCredentials lokiCredentials = null;
81+
if (!string.IsNullOrWhiteSpace(logOptions.GrafanaLokiUsername) && !string.IsNullOrWhiteSpace(logOptions.GrafanaLokiPassword))
82+
{
83+
lokiCredentials = new LokiCredentials
84+
{
85+
Login = logOptions.GrafanaLokiUsername,
86+
Password = logOptions.GrafanaLokiPassword,
87+
};
88+
}
89+
90+
logger.WriteTo.GrafanaLoki(logOptions.GrafanaLokiUrl, grafanaLokiLabels, null, lokiCredentials);
91+
}
92+
6693
configurationAction?.Invoke(logger);
6794
switch (logOptions.LogEventLevel)
6895
{

0 commit comments

Comments
 (0)