Skip to content

Commit

Permalink
Added logging common package
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed Aug 13, 2024
1 parent e3c52ed commit d9eefd4
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release-logging-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: SOFTURE COMMON LOGGING - RELEASE NEW VERSION TO NUGET.ORG

on:
release:
types: [released]

jobs:
publishing:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: ⚙️ Install dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: 8.0.100

- name: 🔗 Restore dependencies
run: dotnet restore ./API

- name: 📂 Create new nuget package
run: dotnet pack --no-restore -c Release -o ./artifacts /p:PackageVersion=${{ github.ref_name }} /p:Version=${{ github.ref_name }} ./API/SOFTURE.Common.Logging/SOFTURE.Common.Logging.csproj
continue-on-error: false

- name: 🚀 Push new nuget package
run: dotnet nuget push ./artifacts/SOFTURE.Common.Logging.${{ github.ref_name }}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
6 changes: 6 additions & 0 deletions API/API.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.Common.Correlation"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.MessageBroker.Rabbit", "SOFTURE.MessageBroker.Rabbit\SOFTURE.MessageBroker.Rabbit.csproj", "{D64C8209-20BA-4294-89B3-A67BB3B9F64C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.Common.Logging", "SOFTURE.Common.Logging\SOFTURE.Common.Logging.csproj", "{956137F1-897C-453F-BB74-A0BA01594C1E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -24,5 +26,9 @@ Global
{D64C8209-20BA-4294-89B3-A67BB3B9F64C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D64C8209-20BA-4294-89B3-A67BB3B9F64C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D64C8209-20BA-4294-89B3-A67BB3B9F64C}.Release|Any CPU.Build.0 = Release|Any CPU
{956137F1-897C-453F-BB74-A0BA01594C1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{956137F1-897C-453F-BB74-A0BA01594C1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{956137F1-897C-453F-BB74-A0BA01594C1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{956137F1-897C-453F-BB74-A0BA01594C1E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
44 changes: 44 additions & 0 deletions API/SOFTURE.Common.Logging/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using SOFTURE.Common.HealthCheck;
using SOFTURE.Common.Logging.HealthChecks;
using SOFTURE.Common.Logging.Settings;
using SOFTURE.Settings.Extensions;

namespace SOFTURE.Common.Logging;

public static class DependencyInjection
{
private static string ApplicationName() => Assembly.GetEntryAssembly()?.GetName().Name ?? "Unknown";

public static IServiceCollection AddCommonLogging<TSettings>(this IServiceCollection services)
where TSettings : ISeqSettings
{
var settings = services.GetSettings<TSettings, SeqSettings>(x => x.Seq);

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Enrich.WithProperty("Application", $"{ApplicationName()}")
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.Seq(settings.Url, apiKey: settings.ApiKey)
.CreateLogger();

Log.Information("{ApplicationName} - Application starting up", ApplicationName());

services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddSerilog();
});

services.AddCommonHealthCheck<SeqHealthCheck>();

return services;
}
}
50 changes: 50 additions & 0 deletions API/SOFTURE.Common.Logging/Extensions/LogExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using CSharpFunctionalExtensions;
using Microsoft.Extensions.Logging;
using SOFTURE.Results;

namespace SOFTURE.Common.Logging.Extensions;

public static class LogExtensions
{
public static Result<T> LogError<T>(this Result<T> result, ILogger logger)
{
if (!result.IsFailure) return result;

var error = Error.Parse(result.Error);

logger.LogError("{ErrorType} - {ErrorDescription}", error.Type, error.Description);

return result;
}

public static Result LogError(this Result result, ILogger logger)
{
if (!result.IsFailure) return result;

var error = Error.Parse(result.Error);

logger.LogError("{ErrorType} - {ErrorDescription}", error.Type, error.Description);

return result;
}

public static Result<T> Log<T>(this Result<T> result, Func<T, string> successMessage, ILogger logger)
{
if (result.IsSuccess)
{
logger.LogInformation("Success: {SuccessMessage}", successMessage(result.Value));
}

return result;
}

public static Result Log(this Result result, string successMessage, ILogger logger)
{
if (result.IsSuccess)
{
logger.LogInformation("Success: {SuccessMessage}", successMessage);
}

return result;
}
}
23 changes: 23 additions & 0 deletions API/SOFTURE.Common.Logging/HealthChecks/SeqHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Net;
using CSharpFunctionalExtensions;
using Microsoft.Extensions.Options;
using SOFTURE.Common.HealthCheck.Core;
using SOFTURE.Common.Logging.Settings;

namespace SOFTURE.Common.Logging.HealthChecks;

internal sealed class SeqHealthCheck(HttpClient httpClient, IOptions<SeqSettings> settings) : CheckBase
{
protected override async Task<Result> Check()
{
var seqSettings = settings.Value;

var uri = new UriBuilder(seqSettings.Url) { Path = "/" }.Uri;

var response = await httpClient.GetAsync(uri);

return response.StatusCode == HttpStatusCode.NotFound
? Result.Success()
: Result.Failure("Seq is not available");
}
}
56 changes: 56 additions & 0 deletions API/SOFTURE.Common.Logging/SOFTURE.Common.Logging.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Serilog" Version="4.0.1"/>
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1"/>
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.0"/>
<PackageReference Include="SOFTURE.Common.HealthCheck" Version="0.0.5" />
<PackageReference Include="SOFTURE.Results" Version="0.0.9" />
<PackageReference Include="SOFTURE.Settings" Version="0.0.9" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="Serilog" Version="4.0.1"/>
<PackageReference Include="Serilog.AspNetCore" Version="6.0.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1"/>
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.0"/>
<PackageReference Include="SOFTURE.Common.HealthCheck" Version="0.0.5" />
<PackageReference Include="SOFTURE.Results" Version="0.0.9" />
<PackageReference Include="SOFTURE.Settings" Version="0.0.9" />
</ItemGroup>

<PropertyGroup>
<AssemblyName>SOFTURE.Common.Logging</AssemblyName>
<AssemblyTitle>$(AssemblyName)</AssemblyTitle>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<Title>$(AssemblyName)</Title>
<Authors>SOFTURE</Authors>
<Copyright>Copyright (c) 2024 $(Authors)</Copyright>
<Description>SOFTURE - Logging</Description>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<PackageId>$(AssemblyName)</PackageId>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/SOFTURE/API</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>See $(PackageProjectUrl)/blob/master/CHANGELOG.md for release notes.</PackageReleaseNotes>
<PackageTags>SOFTURE</PackageTags>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RepositoryType>Git</RepositoryType>
<RepositoryUrl>$(PackageProjectUrl)</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\"/>
<None Include="..\..\LICENSE" Pack="true" PackagePath="\"/>
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions API/SOFTURE.Common.Logging/Settings/ISeqSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SOFTURE.Common.Logging.Settings;

public interface ISeqSettings
{
SeqSettings Seq { get; init; }
}
14 changes: 14 additions & 0 deletions API/SOFTURE.Common.Logging/Settings/SeqSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace SOFTURE.Common.Logging.Settings;

public sealed class SeqSettings
{
#if NET8_0
public required string Url { get; init; }
public required string ApiKey { get; init; }
#endif

#if NET6_0
public string Url { get; init; } = null!;
public string ApiKey { get; init; } = null!;
#endif
}

0 comments on commit d9eefd4

Please sign in to comment.