Skip to content

Commit ae754b1

Browse files
authored
Merge pull request #43 from PandaTechAM/development
compact json support
2 parents 9c4d554 + eaf58e3 commit ae754b1

File tree

6 files changed

+36
-73
lines changed

6 files changed

+36
-73
lines changed

Readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ By leveraging this shared kernel, we aim to:
1818
This package currently supports:
1919

2020
- **OpenAPI Configuration** with SwaggerUI and Scalar.
21-
- **Logging** with Serilog (including ECS or Loki JSON file output, plus automatic log cleanup).
21+
- **Logging** with Serilog (including ECS, Loki and compact JSON file output, plus automatic log cleanup).
2222
- **MediatR and FluentValidation** configurations.
2323
- **Cors Configuration** with easy configuration options.
2424
- **Resilience Pipelines** for `HttpClient` operations.
@@ -270,7 +270,8 @@ Based on the above configuration, the UI will be accessible at the following URL
270270
- **Log Backend Option** Choose between:
271271
- `LogBackend.None` (disables file logging completely),
272272
- `LogBackend.ElasticSearch` (ECS formatter to file), or
273-
- `LogBackend.Loki` (Loki formatter to file).
273+
- `LogBackend.Loki` (Loki formatter to file), or
274+
- `LogBackend.CompactJson` (compact JSON format to file).
274275
- **Environment-Specific Configuration:**
275276
- **Local:** Logs to console.
276277
- **Production:** Logs to file (in ECS or Loki format depending on the backend).
@@ -289,7 +290,7 @@ Based on the above configuration, the UI will be accessible at the following URL
289290

290291
Use the `AddSerilog` extension when building your `WebApplicationBuilder`. You can specify:
291292

292-
- `logBackend`: One of `None`, `ElasticSearch` (ECS file format), or `Loki` (Loki JSON file format).
293+
- `logBackend`: One of `None`, `ElasticSearch` (ECS file format), `Loki` (Loki JSON file format) or `CompactJson`.
293294
- `daysToRetain`: Number of days to keep log files. Older files are automatically removed by the background hosted
294295
service.
295296

src/SharedKernel/Extensions/ConfigurationExtensions.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ internal static class ConfigurationExtensions
88
private const string PersistentConfigurationPath = "PersistentStorage";
99
private const string RepositoryNameConfigurationPath = "RepositoryName";
1010
private const string TimeZoneConfigurationPath = "DefaultTimeZone";
11-
private const string RedisConfigurationPath = "Redis";
12-
private const string LokiConfigurationPath = "Loki";
1311

1412
internal static string GetAllowedCorsOrigins(this IConfiguration configuration)
1513
{
@@ -54,27 +52,4 @@ public static string GetDefaultTimeZone(this IConfiguration configuration)
5452

5553
return timeZone;
5654
}
57-
58-
public static string GetRedisUrl(this IConfiguration configuration)
59-
{
60-
var redisConnectionString = configuration.GetConnectionString(RedisConfigurationPath);
61-
if (redisConnectionString is null)
62-
{
63-
throw new InvalidOperationException("Redis connection string is not configured.");
64-
}
65-
66-
return redisConnectionString;
67-
}
68-
69-
public static string GetLokiUrl(this IConfiguration configuration)
70-
{
71-
var lokiUrl = configuration.GetConnectionString(LokiConfigurationPath);
72-
73-
if (lokiUrl is null)
74-
{
75-
throw new InvalidOperationException("Loki URL is not configured.");
76-
}
77-
78-
return lokiUrl;
79-
}
8055
}

src/SharedKernel/Logging/LogBackend.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public enum LogBackend
44
{
55
None = 1,
66
ElasticSearch = 2,
7-
Loki = 3
7+
Loki = 3,
8+
CompactJson = 4
89
}

src/SharedKernel/Logging/SerilogExtensions.cs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
using Elastic.CommonSchema.Serilog;
1+
using System.Diagnostics;
2+
using Elastic.CommonSchema.Serilog;
23
using Microsoft.AspNetCore.Builder;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Hosting;
56
using Microsoft.Extensions.Logging;
67
using Serilog;
78
using Serilog.Events;
9+
using Serilog.Formatting;
10+
using Serilog.Formatting.Compact;
811
using Serilog.Sinks.Grafana.Loki;
912
using SharedKernel.Extensions;
1013

@@ -70,46 +73,31 @@ private static LoggerConfiguration ConfigureDestinations(this LoggerConfiguratio
7073
loggerConfig.WriteTo.Async(a => a.Console());
7174
}
7275

73-
switch (logBackend)
76+
if (logBackend != LogBackend.None)
7477
{
75-
case LogBackend.None:
76-
break;
77-
78-
case LogBackend.ElasticSearch:
79-
loggerConfig.WriteToEcsFileAsync(builder);
80-
break;
81-
82-
case LogBackend.Loki:
83-
loggerConfig.WriteToLokiAsync(builder);
84-
break;
85-
default:
86-
throw new ArgumentOutOfRangeException(nameof(logBackend), logBackend, null);
78+
loggerConfig.WriteToFileAsync(builder, logBackend);
8779
}
8880

8981
return loggerConfig;
9082
}
9183

92-
private static LoggerConfiguration WriteToEcsFileAsync(this LoggerConfiguration loggerConfig,
93-
WebApplicationBuilder builder)
84+
private static LoggerConfiguration WriteToFileAsync(this LoggerConfiguration loggerConfig,
85+
WebApplicationBuilder builder,
86+
LogBackend logBackend)
9487
{
95-
return loggerConfig.WriteTo.Async(a =>
96-
a.File(
97-
new EcsTextFormatter(),
98-
builder.GetLogsPath(),
99-
rollingInterval: RollingInterval.Day
100-
)
101-
);
102-
}
88+
// Choose the formatter based on the selected log backend
89+
ITextFormatter formatter = logBackend switch
90+
{
91+
LogBackend.ElasticSearch => new EcsTextFormatter(),
92+
LogBackend.Loki => new LokiJsonTextFormatter(),
93+
LogBackend.CompactJson => new CompactJsonFormatter(),
94+
_ => new CompactJsonFormatter() // Fallback
95+
};
10396

104-
private static LoggerConfiguration WriteToLokiAsync(this LoggerConfiguration loggerConfig,
105-
WebApplicationBuilder builder)
106-
{
10797
return loggerConfig.WriteTo.Async(a =>
108-
a.File(
109-
new LokiJsonTextFormatter(),
98+
a.File(formatter,
11099
builder.GetLogsPath(),
111-
rollingInterval: RollingInterval.Day
112-
)
100+
rollingInterval: RollingInterval.Day)
113101
);
114102
}
115103

@@ -155,9 +143,7 @@ private static bool ShouldExcludeSwaggerLogs(this LogEvent logEvent)
155143
private static bool ShouldExcludeMassTransitHealthCheckLogs(this LogEvent logEvent)
156144
{
157145
var message = logEvent.RenderMessage();
158-
return message.Contains("Health check masstransit-bus", StringComparison.OrdinalIgnoreCase)
159-
&& message.Contains("Unhealthy", StringComparison.OrdinalIgnoreCase)
160-
&& message.Contains("Not ready: not started", StringComparison.OrdinalIgnoreCase);
146+
return message.StartsWith("Health check masstransit-bus with status Unhealthy completed after");
161147
}
162148

163149
#endregion

src/SharedKernel/Logging/StartupLoggerExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static WebApplicationBuilder LogStartAttempt(this WebApplicationBuilder b
1313
var now = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
1414

1515
Console.ForegroundColor = ConsoleColor.DarkYellow;
16-
Console.WriteLine("============================================================");
16+
Console.WriteLine("===============================================================");
1717
Console.ResetColor();
1818

1919
Console.WriteLine("APPLICATION START ATTEMPT");
@@ -24,7 +24,7 @@ public static WebApplicationBuilder LogStartAttempt(this WebApplicationBuilder b
2424
Console.WriteLine($"Machine Name : {Environment.MachineName}");
2525

2626
Console.ForegroundColor = ConsoleColor.DarkYellow;
27-
Console.WriteLine("============================================================");
27+
Console.WriteLine("===============================================================");
2828
Console.ResetColor();
2929

3030
return builder;
@@ -37,15 +37,15 @@ public static WebApplication LogStartSuccess(this WebApplication app)
3737
var now = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
3838

3939
Console.ForegroundColor = ConsoleColor.DarkGreen;
40-
Console.WriteLine("\n============================================================");
40+
Console.WriteLine("\n===============================================================");
4141
Console.ResetColor();
4242

4343
Console.WriteLine("APPLICATION START SUCCESS");
4444
Console.WriteLine($"Timestamp : {now}");
4545
Console.WriteLine($"Initialization : {deltaInSeconds} seconds");
4646

4747
Console.ForegroundColor = ConsoleColor.DarkGreen;
48-
Console.WriteLine("============================================================");
48+
Console.WriteLine("===============================================================");
4949
Console.ResetColor();
5050

5151
return app;
@@ -57,15 +57,15 @@ public static WebApplicationBuilder LogModuleRegistrationSuccess(
5757
var now = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
5858

5959
Console.ForegroundColor = ConsoleColor.DarkYellow;
60-
Console.WriteLine("\n============================================================");
60+
Console.WriteLine("\n===============================================================");
6161
Console.ResetColor();
6262

6363
Console.WriteLine("MODULE REGISTRATION SUCCESS");
6464
Console.WriteLine($"Timestamp : {now}");
6565
Console.WriteLine($"Module Name : {moduleName}");
6666

6767
Console.ForegroundColor = ConsoleColor.DarkYellow;
68-
Console.WriteLine("============================================================");
68+
Console.WriteLine("===============================================================");
6969
Console.ResetColor();
7070

7171
return builder;
@@ -76,15 +76,15 @@ public static WebApplication LogModuleUseSuccess(this WebApplication app, string
7676
var now = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
7777

7878
Console.ForegroundColor = ConsoleColor.DarkYellow;
79-
Console.WriteLine("\n============================================================");
79+
Console.WriteLine("\n===============================================================");
8080
Console.ResetColor();
8181

8282
Console.WriteLine("MODULE USE SUCCESS");
8383
Console.WriteLine($"Timestamp : {now}");
8484
Console.WriteLine($"Module Name : {moduleName}");
8585

8686
Console.ForegroundColor = ConsoleColor.DarkYellow;
87-
Console.WriteLine("============================================================");
87+
Console.WriteLine("===============================================================");
8888
Console.ResetColor();
8989

9090
return app;

src/SharedKernel/SharedKernel.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.3.0</Version>
11+
<Version>1.3.1</Version>
1212
<PackageId>Pandatech.SharedKernel</PackageId>
1313
<Title>Pandatech Shared Kernel Library</Title>
1414
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
1515
<Description>Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel</RepositoryUrl>
17-
<PackageReleaseNotes>Logging upgrade</PackageReleaseNotes>
17+
<PackageReleaseNotes>Added compact json support to log backend</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)