Skip to content

Commit de60033

Browse files
committed
Feat: Common Console Config
1 parent fade4de commit de60033

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed

src/CodeOfChaos.Extensions.Serilog/CodeOfChaos.Extensions.Serilog.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25+
<PackageReference Include="CodeOfChaos.Ansi" Version="0.1.7" />
2526
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
2627
<PackageReference Include="Serilog" Version="4.2.0" />
28+
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
29+
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
2730
</ItemGroup>
2831

2932
<ItemGroup>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
namespace CodeOfChaos.Extensions.Serilog;
5+
6+
// ---------------------------------------------------------------------------------------------------------------------
7+
// Code
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
public static class ConsoleOutputTemplates {
10+
public const string Default = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";
11+
public const string DefaultShort = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}";
12+
public const string AnnaSasDevServer = "[{Timestamp:HH:mm:ss} {Level:u3} {Section,-8}] {Message:lj}{NewLine}";
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Serilog.Sinks.SystemConsole.Themes;
5+
using CodeOfChaos.Ansi;
6+
7+
namespace CodeOfChaos.Extensions.Serilog;
8+
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
// Code
11+
// ---------------------------------------------------------------------------------------------------------------------
12+
public static class ConsoleThemes {
13+
public static readonly AnsiConsoleTheme AnnaSasDevTheme = new(
14+
new Dictionary<ConsoleThemeStyle, string> {
15+
[ConsoleThemeStyle.Text] = AnsiColor.AsFore("white"),
16+
[ConsoleThemeStyle.SecondaryText] = AnsiColor.AsFore("silver"),
17+
[ConsoleThemeStyle.TertiaryText] = AnsiColor.AsFore("gray"),
18+
[ConsoleThemeStyle.Invalid] = AnsiColor.AsFore("gold"),
19+
[ConsoleThemeStyle.Null] = AnsiColor.AsFore("coral"),
20+
[ConsoleThemeStyle.Name] = AnsiColor.AsFore("slategray"),
21+
[ConsoleThemeStyle.String] = AnsiColor.AsFore("aqua"),
22+
[ConsoleThemeStyle.Number] = AnsiColor.AsFore("mediumpurple"),
23+
[ConsoleThemeStyle.Boolean] = AnsiColor.AsFore("coral"),
24+
[ConsoleThemeStyle.Scalar] = AnsiColor.AsFore("coral"),
25+
[ConsoleThemeStyle.LevelVerbose] = AnsiColor.AsFore("silver"),
26+
[ConsoleThemeStyle.LevelDebug] = AnsiColor.AsFore("rose"),
27+
[ConsoleThemeStyle.LevelInformation] = AnsiColor.AsFore("white"),
28+
[ConsoleThemeStyle.LevelWarning] = AnsiColor.AsFore("gold"),
29+
[ConsoleThemeStyle.LevelError] = AnsiColor.AsFore("white") + AnsiColor.AsBack("rose"),
30+
[ConsoleThemeStyle.LevelFatal] = AnsiColor.AsFore("white") + AnsiColor.AsBack("maroon")
31+
});
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Serilog.Core;
5+
using Serilog.Events;
6+
7+
namespace CodeOfChaos.Extensions.Serilog.Enrichers;
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class PaddedSectionEnricher : ILogEventEnricher {
12+
public const int MaxLength = 8;
13+
14+
// -----------------------------------------------------------------------------------------------------------------
15+
// Methods
16+
// -----------------------------------------------------------------------------------------------------------------
17+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) {
18+
if (!logEvent.Properties.TryGetValue("Section", out LogEventPropertyValue? sectionProperty)) {
19+
// If "Section" is not defined, fallback to default value
20+
sectionProperty = new ScalarValue(string.Empty);
21+
}
22+
23+
string sectionValue = sectionProperty.ToString().Trim('"'); // Remove quotes and trim
24+
25+
// Left-pad as required to a max of 8 characters
26+
string paddedSection = sectionValue.PadLeft(MaxLength)[..MaxLength];
27+
28+
LogEventProperty paddedProperty = propertyFactory.CreateProperty("Section", paddedSection);
29+
logEvent.AddOrUpdateProperty(paddedProperty);
30+
}
31+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Extensions.Serilog.Enrichers;
5+
using Serilog;
6+
using Serilog.Core;
7+
using Serilog.Events;
8+
using Serilog.Sinks.SystemConsole.Themes;
9+
10+
namespace CodeOfChaos.Extensions.Serilog;
11+
12+
// ---------------------------------------------------------------------------------------------------------------------
13+
// Code
14+
// ---------------------------------------------------------------------------------------------------------------------
15+
public static class LoggerConfigurationExtensions {
16+
public static LoggerConfiguration WriteToAsyncConsole(
17+
this LoggerConfiguration loggerConfiguration,
18+
LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose,
19+
string outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
20+
IFormatProvider? formatProvider = null,
21+
LoggingLevelSwitch? levelSwitch = null,
22+
LogEventLevel? standardErrorFromLevel = null,
23+
ConsoleTheme? theme = null,
24+
bool applyThemeToRedirectedOutput = false,
25+
object? syncRoot = null
26+
) {
27+
loggerConfiguration.WriteTo.Async(lsc => lsc.Console(
28+
restrictedToMinimumLevel,
29+
outputTemplate,
30+
formatProvider,
31+
levelSwitch,
32+
standardErrorFromLevel,
33+
theme,
34+
applyThemeToRedirectedOutput,
35+
syncRoot
36+
));
37+
return loggerConfiguration;
38+
}
39+
40+
public static LoggerConfiguration WithPaddedSectionEnricher(this LoggerConfiguration loggerConfiguration) {
41+
return loggerConfiguration.Enrich.With<PaddedSectionEnricher>();
42+
}
43+
44+
// -----------------------------------------------------------------------------------------------------------------
45+
// Opinionated configurations
46+
// -----------------------------------------------------------------------------------------------------------------
47+
public static LoggerConfiguration AsAnnaSasDevServerConsole(this LoggerConfiguration loggerConfiguration) {
48+
return loggerConfiguration
49+
.WithPaddedSectionEnricher()
50+
.WriteToAsyncConsole(
51+
outputTemplate: ConsoleOutputTemplates.AnnaSasDevServer,
52+
theme: ConsoleThemes.AnnaSasDevTheme
53+
);
54+
}
55+
}

src/CodeOfChaos.Extensions.Serilog/LoggerExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,7 @@ public static void ExitFatal(this ILogger logger, int exitCode, string messageTe
104104
logger.Fatal(messageTemplate, propertyValues);
105105
throw new ExitApplicationException(exitCode, messageTemplate);
106106
}
107+
108+
109+
public static ILogger ForSectionProperty(this ILogger logger, string sectionName) => logger.ForContext("Section", sectionName);
107110
}

0 commit comments

Comments
 (0)