From e4695ad19dacf6a4ccd5ed2a61ff3d8256696337 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 4 Nov 2024 03:31:21 +0000 Subject: [PATCH 1/7] Add serilog enricher unit test --- .../ClassNameEnricherTests.cs | 33 +++++++++++++++++ .../Serilog.Enrichers.ClassName.Tests.csproj | 35 +++++++++++++++++++ src/Serilog.Enrichers.ClassName.sln | 8 ++++- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/Serilog.Enrichers.ClassName.Tests/ClassNameEnricherTests.cs create mode 100644 src/Serilog.Enrichers.ClassName.Tests/Serilog.Enrichers.ClassName.Tests.csproj diff --git a/src/Serilog.Enrichers.ClassName.Tests/ClassNameEnricherTests.cs b/src/Serilog.Enrichers.ClassName.Tests/ClassNameEnricherTests.cs new file mode 100644 index 0000000..d72697a --- /dev/null +++ b/src/Serilog.Enrichers.ClassName.Tests/ClassNameEnricherTests.cs @@ -0,0 +1,33 @@ +using Serilog.Sinks.TestCorrelator; + +namespace Serilog.Enrichers.ClassName.Tests +{ + public class ClassNameEnricherTests + { + [Fact] + public void ClassNameProperty_MustExistAndMatch() + { + // Arrange + const string expectedClassName = "ClassNameEnricher"; + var logger = new LoggerConfiguration() + .Enrich.With(new ClassNameEnricher()) + .WriteTo.TestCorrelator() + .CreateLogger() + .ForContext("SourceContext", typeof(ClassNameEnricher).FullName); + + using (TestCorrelator.CreateContext()) + { + // Act + logger.Information("Test log"); + + // Assert + var logEvent = TestCorrelator.GetLogEventsFromCurrentContext().Single(); + Assert.NotNull(logEvent); + + // Verify if the UserId property is added + Assert.True(logEvent.Properties.ContainsKey("ClassName")); + Assert.Equal(expectedClassName, logEvent.Properties["ClassName"].ToString().Trim('"')); + } + } + } +} \ No newline at end of file diff --git a/src/Serilog.Enrichers.ClassName.Tests/Serilog.Enrichers.ClassName.Tests.csproj b/src/Serilog.Enrichers.ClassName.Tests/Serilog.Enrichers.ClassName.Tests.csproj new file mode 100644 index 0000000..95d173f --- /dev/null +++ b/src/Serilog.Enrichers.ClassName.Tests/Serilog.Enrichers.ClassName.Tests.csproj @@ -0,0 +1,35 @@ + + + + net8.0 + enable + enable + + false + true + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/src/Serilog.Enrichers.ClassName.sln b/src/Serilog.Enrichers.ClassName.sln index 4f0eb9f..92ddf00 100644 --- a/src/Serilog.Enrichers.ClassName.sln +++ b/src/Serilog.Enrichers.ClassName.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.5.33516.290 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{D12BECA3-8DFB-48E5-9430-3C060D774BD6}") = "Serilog.Enrichers.ClassName", "Serilog.Enrichers.ClassName\Serilog.Enrichers.ClassName.csproj", "{C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.ClassName", "Serilog.Enrichers.ClassName\Serilog.Enrichers.ClassName.csproj", "{C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.ClassName.Tests", "Serilog.Enrichers.ClassName.Tests\Serilog.Enrichers.ClassName.Tests.csproj", "{1964BEA8-3030-4E02-AD97-87D65E16157A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}.Debug|Any CPU.Build.0 = Debug|Any CPU {C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}.Release|Any CPU.ActiveCfg = Release|Any CPU {C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}.Release|Any CPU.Build.0 = Release|Any CPU + {1964BEA8-3030-4E02-AD97-87D65E16157A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1964BEA8-3030-4E02-AD97-87D65E16157A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1964BEA8-3030-4E02-AD97-87D65E16157A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1964BEA8-3030-4E02-AD97-87D65E16157A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 9bbcefbfe931033a1ac63468fc64afed3b6a530e Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 5 Nov 2024 15:09:43 +0000 Subject: [PATCH 2/7] Add demo program --- .../DemoClass.cs | 14 ++++++++++++ .../Program.cs | 22 +++++++++++++++++++ .../Serilog.Enrichers.ClassName.Demo.csproj | 19 ++++++++++++++++ src/Serilog.Enrichers.ClassName.sln | 8 ++++++- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/Serilog.Enrichers.ClassName.Demo/DemoClass.cs create mode 100644 src/Serilog.Enrichers.ClassName.Demo/Program.cs create mode 100644 src/Serilog.Enrichers.ClassName.Demo/Serilog.Enrichers.ClassName.Demo.csproj diff --git a/src/Serilog.Enrichers.ClassName.Demo/DemoClass.cs b/src/Serilog.Enrichers.ClassName.Demo/DemoClass.cs new file mode 100644 index 0000000..0012eba --- /dev/null +++ b/src/Serilog.Enrichers.ClassName.Demo/DemoClass.cs @@ -0,0 +1,14 @@ +using Serilog; + +namespace SerilogDemo +{ + internal class DemoClass + { + private static readonly ILogger Log = Serilog.Log.ForContext(); + + public void DoWork() + { + Log.Information("Doing some work..."); + } + } +} diff --git a/src/Serilog.Enrichers.ClassName.Demo/Program.cs b/src/Serilog.Enrichers.ClassName.Demo/Program.cs new file mode 100644 index 0000000..7c8904c --- /dev/null +++ b/src/Serilog.Enrichers.ClassName.Demo/Program.cs @@ -0,0 +1,22 @@ +// See https://aka.ms/new-console-template for more information +// Configure Serilog with SourceContext +using Serilog; +using Serilog.Enrichers.ClassName; +using SerilogDemo; + + +Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .Enrich.WithClassName() + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{ClassName}] [{SourceContext}] {Message:lj}{NewLine}{Exception}") + .CreateLogger(); + + Log.Information("Application starting..."); + +// Log from another class +var demoClass = new DemoClass(); + demoClass.DoWork(); + +Log.Information("Application ending..."); +Log.CloseAndFlush(); + diff --git a/src/Serilog.Enrichers.ClassName.Demo/Serilog.Enrichers.ClassName.Demo.csproj b/src/Serilog.Enrichers.ClassName.Demo/Serilog.Enrichers.ClassName.Demo.csproj new file mode 100644 index 0000000..84467c9 --- /dev/null +++ b/src/Serilog.Enrichers.ClassName.Demo/Serilog.Enrichers.ClassName.Demo.csproj @@ -0,0 +1,19 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/src/Serilog.Enrichers.ClassName.sln b/src/Serilog.Enrichers.ClassName.sln index 92ddf00..f2eaf5a 100644 --- a/src/Serilog.Enrichers.ClassName.sln +++ b/src/Serilog.Enrichers.ClassName.sln @@ -5,7 +5,9 @@ VisualStudioVersion = 17.5.33516.290 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.ClassName", "Serilog.Enrichers.ClassName\Serilog.Enrichers.ClassName.csproj", "{C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.ClassName.Tests", "Serilog.Enrichers.ClassName.Tests\Serilog.Enrichers.ClassName.Tests.csproj", "{1964BEA8-3030-4E02-AD97-87D65E16157A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.ClassName.Tests", "Serilog.Enrichers.ClassName.Tests\Serilog.Enrichers.ClassName.Tests.csproj", "{1964BEA8-3030-4E02-AD97-87D65E16157A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.ClassName.Demo", "Serilog.Enrichers.ClassName.Demo\Serilog.Enrichers.ClassName.Demo.csproj", "{B92B7663-6086-44DA-B1FE-ACEFF082D8CF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {1964BEA8-3030-4E02-AD97-87D65E16157A}.Debug|Any CPU.Build.0 = Debug|Any CPU {1964BEA8-3030-4E02-AD97-87D65E16157A}.Release|Any CPU.ActiveCfg = Release|Any CPU {1964BEA8-3030-4E02-AD97-87D65E16157A}.Release|Any CPU.Build.0 = Release|Any CPU + {B92B7663-6086-44DA-B1FE-ACEFF082D8CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B92B7663-6086-44DA-B1FE-ACEFF082D8CF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B92B7663-6086-44DA-B1FE-ACEFF082D8CF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B92B7663-6086-44DA-B1FE-ACEFF082D8CF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 9cf7d6737df4050a094acde9cd35c04ac24ed173 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 5 Nov 2024 15:34:36 +0000 Subject: [PATCH 3/7] Add extension method that obtain top level class using stacktrace --- .../LoggingExtensions.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Serilog.Enrichers.ClassName/LoggingExtensions.cs b/src/Serilog.Enrichers.ClassName/LoggingExtensions.cs index ce8f21d..fdf36a9 100644 --- a/src/Serilog.Enrichers.ClassName/LoggingExtensions.cs +++ b/src/Serilog.Enrichers.ClassName/LoggingExtensions.cs @@ -1,13 +1,26 @@ using Serilog.Configuration; +using System.Diagnostics; namespace Serilog.Enrichers.ClassName { public static class LoggingExtensions - { + { + public static LoggerConfiguration WithClassName(this LoggerEnrichmentConfiguration enrich) where T : class + { + _ = enrich ?? throw new ArgumentNullException(nameof(enrich)); + + enrich.WithProperty("SourceContext", typeof(T).FullName); + return enrich.With(); + } + public static LoggerConfiguration WithClassName(this LoggerEnrichmentConfiguration enrich) { _ = enrich ?? throw new ArgumentNullException(nameof(enrich)); + var stackFrame = new StackTrace().GetFrame(1); + var declaringType = stackFrame?.GetMethod()?.DeclaringType; + + enrich.WithProperty("SourceContext", declaringType?.FullName ?? "Unknown"); return enrich.With(); } } From 800b1291d26d271ad7b9d7667491095d68fc4d84 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 5 Nov 2024 15:34:52 +0000 Subject: [PATCH 4/7] Add nuget version label to read me file --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a6afa7e..8e38444 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![Build](https://github.com/Jandini/serilog-enrichers-classname/actions/workflows/build.yml/badge.svg)](https://github.com/Jandini/serilog-enrichers-classname/actions/workflows/build.yml) [![NuGet](https://github.com/Jandini/serilog-enrichers-classname/actions/workflows/nuget.yml/badge.svg)](https://github.com/Jandini/serilog-enrichers-classname/actions/workflows/nuget.yml) +[![NuGet Version](http://img.shields.io/nuget/v/Serilog.Enrichers.ClassName?style=flat&label=Version)](https://www.nuget.org/packages/Serilog.Enrichers.ClassName/) Enrich Serilog logs with class name only. From 96a0174b1e6b07cf97c9296c730f2dec3846f26b Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 5 Nov 2024 15:37:13 +0000 Subject: [PATCH 5/7] Upgrade dotnet version in github actions --- .github/workflows/build.yml | 2 +- .github/workflows/nuget.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 69c0ca5..7eb1bfc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,7 +28,7 @@ jobs: - name: Setup uses: actions/setup-dotnet@v1 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json - name: Restore diff --git a/.github/workflows/nuget.yml b/.github/workflows/nuget.yml index 58cd4d9..f57b058 100644 --- a/.github/workflows/nuget.yml +++ b/.github/workflows/nuget.yml @@ -25,7 +25,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v1 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json - name: Restore From 1c851611de827489a9f520ff28cee8fb3c80bab0 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 5 Nov 2024 15:37:25 +0000 Subject: [PATCH 6/7] Add namespace and class name to demo project --- .../Program.cs | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Serilog.Enrichers.ClassName.Demo/Program.cs b/src/Serilog.Enrichers.ClassName.Demo/Program.cs index 7c8904c..734dee1 100644 --- a/src/Serilog.Enrichers.ClassName.Demo/Program.cs +++ b/src/Serilog.Enrichers.ClassName.Demo/Program.cs @@ -4,19 +4,25 @@ using Serilog.Enrichers.ClassName; using SerilogDemo; +namespace Demo; -Log.Logger = new LoggerConfiguration() - .MinimumLevel.Debug() - .Enrich.WithClassName() - .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{ClassName}] [{SourceContext}] {Message:lj}{NewLine}{Exception}") - .CreateLogger(); +class MyProgram +{ + public static void Main(string[] args) + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .Enrich.WithClassName() + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{ClassName}] [{SourceContext}] {Message:lj}{NewLine}{Exception}") + .CreateLogger(); - Log.Information("Application starting..."); + Log.Information("Application starting..."); -// Log from another class -var demoClass = new DemoClass(); - demoClass.DoWork(); + // Log from another class + var demoClass = new DemoClass(); + demoClass.DoWork(); -Log.Information("Application ending..."); -Log.CloseAndFlush(); - + Log.Information("Application ending..."); + Log.CloseAndFlush(); + } +} From 8fe1cda492c03531f0c807ccf390bdd451085c43 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 5 Nov 2024 15:46:38 +0000 Subject: [PATCH 7/7] Add unit tests to github actions --- .github/workflows/build.yml | 3 +++ .github/workflows/nuget.yml | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7eb1bfc..2158dac 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,6 +33,9 @@ jobs: - name: Restore run: dotnet restore + + - name: Test + run: dotnet test --no-restore --verbosity normal - name: Pack run: dotnet pack --no-restore -c:Release diff --git a/.github/workflows/nuget.yml b/.github/workflows/nuget.yml index f57b058..4ffbcb0 100644 --- a/.github/workflows/nuget.yml +++ b/.github/workflows/nuget.yml @@ -29,8 +29,11 @@ jobs: source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json - name: Restore - run: dotnet restore + run: dotnet restore + - name: Test + run: dotnet test --no-restore --verbosity normal + - name: Pack run: dotnet pack --no-restore -c:Release