-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |