Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support top level statements #4

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ 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
run: dotnet restore

- name: Test
run: dotnet test --no-restore --verbosity normal

- name: Pack
run: dotnet pack --no-restore -c:Release
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ 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
run: dotnet restore
run: dotnet restore

- name: Test
run: dotnet test --no-restore --verbosity normal

- name: Pack
run: dotnet pack --no-restore -c:Release

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
14 changes: 14 additions & 0 deletions src/Serilog.Enrichers.ClassName.Demo/DemoClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Serilog;

namespace SerilogDemo
{
internal class DemoClass
{
private static readonly ILogger Log = Serilog.Log.ForContext<DemoClass>();

public void DoWork()
{
Log.Information("Doing some work...");
}
}
}
28 changes: 28 additions & 0 deletions src/Serilog.Enrichers.ClassName.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// See https://aka.ms/new-console-template for more information
// Configure Serilog with SourceContext
using Serilog;
using Serilog.Enrichers.ClassName;
using SerilogDemo;

namespace Demo;

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 from another class
var demoClass = new DemoClass();
demoClass.DoWork();

Log.Information("Application ending...");
Log.CloseAndFlush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Serilog.Enrichers.ClassName\Serilog.Enrichers.ClassName.csproj" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions src/Serilog.Enrichers.ClassName.Tests/ClassNameEnricherTests.cs
Original file line number Diff line number Diff line change
@@ -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('"'));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.TestCorrelator" Version="4.0.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Serilog.Enrichers.ClassName\Serilog.Enrichers.ClassName.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
14 changes: 13 additions & 1 deletion src/Serilog.Enrichers.ClassName.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ 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("{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
Expand All @@ -15,6 +19,14 @@ 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
{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
Expand Down
15 changes: 14 additions & 1 deletion src/Serilog.Enrichers.ClassName/LoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
using Serilog.Configuration;
using System.Diagnostics;

namespace Serilog.Enrichers.ClassName
{
public static class LoggingExtensions
{
{
public static LoggerConfiguration WithClassName<T>(this LoggerEnrichmentConfiguration enrich) where T : class
{
_ = enrich ?? throw new ArgumentNullException(nameof(enrich));

enrich.WithProperty("SourceContext", typeof(T).FullName);
return enrich.With<ClassNameEnricher>();
}

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<ClassNameEnricher>();
}
}
Expand Down
Loading