Skip to content

Commit 4fefbcf

Browse files
authored
Merge pull request #3 from code-of-chaos/import-aterra_engine
Feta: Import from AterraEngine
2 parents ea6a5d3 + 6d7567e commit 4fefbcf

File tree

15 files changed

+263
-13
lines changed

15 files changed

+263
-13
lines changed

src/CodeOfChaos.Extensions.AspNetCore/CodeOfChaos.Extensions.AspNetCore.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31-
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />
31+
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />
3232
<None Include="README.md" Pack="true" PackagePath="" />
33-
<None Include="../../assets/icon.png" Pack="true" PackagePath="" />
33+
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
3434
</ItemGroup>
3535

3636
</Project>

src/CodeOfChaos.Extensions.EntityFrameworkCore/CodeOfChaos.Extensions.EntityFrameworkCore.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<ItemGroup>
2525
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
2626
</ItemGroup>
27-
27+
2828
<ItemGroup>
29-
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />
29+
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />
3030
<None Include="README.md" Pack="true" PackagePath="" />
31-
<None Include="../../assets/icon.png" Pack="true" PackagePath="" />
31+
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
3232
</ItemGroup>
3333
</Project>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
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>
30-
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />
33+
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />
3134
<None Include="README.md" Pack="true" PackagePath="" />
32-
<None Include="../../assets/icon.png" Pack="true" PackagePath="" />
35+
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
3336
</ItemGroup>
3437
</Project>
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 int MaxLength { get; } = 8;
13+
14+
// -----------------------------------------------------------------------------------------------------------------
15+
// Constructors
16+
// -----------------------------------------------------------------------------------------------------------------
17+
public PaddedSectionEnricher() { }
18+
public PaddedSectionEnricher(int maxLength) => MaxLength = maxLength;
19+
20+
// -----------------------------------------------------------------------------------------------------------------
21+
// Methods
22+
// -----------------------------------------------------------------------------------------------------------------
23+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) {
24+
if (!logEvent.Properties.TryGetValue("Section", out LogEventPropertyValue? sectionProperty)) {
25+
// If "Section" is not defined, fallback to default value
26+
sectionProperty = new ScalarValue(string.Empty);
27+
}
28+
29+
string sectionValue = sectionProperty.ToString().Trim('"'); // Remove quotes and trim
30+
31+
// Left-pad as required to a max of 8 characters
32+
string paddedSection = sectionValue.PadLeft(MaxLength)[..MaxLength];
33+
34+
LogEventProperty paddedProperty = propertyFactory.CreateProperty("Section", paddedSection);
35+
logEvent.AddOrUpdateProperty(paddedProperty);
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 TruncateSourceContextEnricher : ILogEventEnricher {
12+
public int MaxLength { get; } = 8;
13+
14+
// -----------------------------------------------------------------------------------------------------------------
15+
// Constructors
16+
// -----------------------------------------------------------------------------------------------------------------
17+
public TruncateSourceContextEnricher() { }
18+
public TruncateSourceContextEnricher(int maxLength) => MaxLength = maxLength;
19+
20+
// -----------------------------------------------------------------------------------------------------------------
21+
// Methods
22+
// -----------------------------------------------------------------------------------------------------------------
23+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) {
24+
if (!logEvent.Properties.TryGetValue("SourceContext", out LogEventPropertyValue? sourceContextValue)
25+
|| sourceContextValue is not ScalarValue { Value: string sourceContext }) return;
26+
27+
string truncatedSourceContext = sourceContext.Length > MaxLength + 3
28+
? string.Concat("...", sourceContext.AsSpan(sourceContext.Length - MaxLength, MaxLength))
29+
: sourceContext;
30+
31+
var truncatedProperty = new LogEventProperty("SourceContext", new ScalarValue(truncatedSourceContext));
32+
logEvent.AddOrUpdateProperty(truncatedProperty);
33+
}
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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, int maxLength = 8)
41+
=> loggerConfiguration.Enrich.With(new PaddedSectionEnricher(maxLength));
42+
43+
public static LoggerConfiguration WithTruncateSourceContextEnricher(this LoggerConfiguration loggerConfiguration, int maxLength = 8)
44+
=> loggerConfiguration.Enrich.With(new TruncateSourceContextEnricher(maxLength));
45+
46+
// -----------------------------------------------------------------------------------------------------------------
47+
// Opinionated configurations
48+
// -----------------------------------------------------------------------------------------------------------------
49+
public static LoggerConfiguration AsAnnaSasDevServerConsole(this LoggerConfiguration loggerConfiguration) {
50+
return loggerConfiguration
51+
.WithPaddedSectionEnricher()
52+
.WriteToAsyncConsole(
53+
outputTemplate: ConsoleOutputTemplates.AnnaSasDevServer,
54+
theme: ConsoleThemes.AnnaSasDevTheme
55+
);
56+
}
57+
}

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
}

src/CodeOfChaos.Extensions/CodeOfChaos.Extensions.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />
26+
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />
2727
<None Include="README.md" Pack="true" PackagePath="" />
28-
<None Include="../../assets/icon.png" Pack="true" PackagePath="" />
28+
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
2929
</ItemGroup>
3030

3131
</Project>

0 commit comments

Comments
 (0)