-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code(Cross-cutting concerns): Integrate Serilog into the project
- Loading branch information
1 parent
762c34d
commit 83c1e02
Showing
14 changed files
with
437 additions
and
11 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
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 |
---|---|---|
@@ -1,9 +1,49 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
"app": { | ||
"name": "Chat Stellar API", | ||
"version": "0.0.1" | ||
}, | ||
"AllowedHosts": "*" | ||
"AllowedHosts": "*", | ||
"serilog": { | ||
"level": "information", | ||
"overrides": { | ||
"Microsoft.AspNetCore": "Warning", | ||
"Microsoft.EntityFrameworkCore.Database.Command": "Warning", | ||
"Microsoft.EntityFrameworkCore.Infrastructure": "Warning" | ||
}, | ||
"excludePaths": [ | ||
"/", | ||
"/metrics", | ||
"/ping" | ||
], | ||
"excludeProperties": [ | ||
"api_key", | ||
"access_key", | ||
"apiKey", | ||
"apiSecret", | ||
"clientId", | ||
"clientSecret", | ||
"connectionString", | ||
"password", | ||
"email", | ||
"login", | ||
"secret", | ||
"token", | ||
"organizationId" | ||
], | ||
"console": { | ||
"enabled": true | ||
}, | ||
"file": { | ||
"enabled": false, | ||
"path": "logs/logs.txt", | ||
"interval": "day" | ||
}, | ||
"seq": { | ||
"enabled": true, | ||
"url": "http://localhost:5341", | ||
"apiKey": "secret" | ||
}, | ||
"tags": {} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Shared/StellarChat.Shared.Abstractions/Contexts/IContext.cs
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,11 @@ | ||
namespace StellarChat.Shared.Abstractions.Contexts; | ||
|
||
public interface IContext | ||
{ | ||
Guid RequestId { get; } | ||
Guid CorrelationId { get; } | ||
string TraceId { get; } | ||
string IpAddress { get; } | ||
string UserAgent { get; } | ||
IIdentityContext Identity { get; } | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Shared/StellarChat.Shared.Abstractions/Contexts/IIdentityContext.cs
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,9 @@ | ||
namespace StellarChat.Shared.Abstractions.Contexts; | ||
|
||
public interface IIdentityContext | ||
{ | ||
bool IsAuthenticated { get; } | ||
public Guid Id { get; } | ||
string Role { get; } | ||
Dictionary<string, IEnumerable<string>> Claims { get; } | ||
} |
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,8 @@ | ||
namespace StellarChat.Shared.Infrastructure; | ||
|
||
internal class AppOptions | ||
{ | ||
public string Name { get; set; } = string.Empty; | ||
public string Instance { get; set; } = string.Empty; | ||
public string Version { get; set; } = string.Empty; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Shared/StellarChat.Shared.Infrastructure/Contexts/Context.cs
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,36 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using StellarChat.Shared.Abstractions.Contexts; | ||
|
||
namespace StellarChat.Shared.Infrastructure.Contexts; | ||
|
||
public class Context : IContext | ||
{ | ||
public Guid RequestId { get; } = Guid.NewGuid(); | ||
public Guid CorrelationId { get; } | ||
public string TraceId { get; } | ||
public string IpAddress { get; } | ||
public string UserAgent { get; } | ||
public IIdentityContext Identity { get; } | ||
|
||
public Context() : this(Guid.NewGuid(), $"{Guid.NewGuid():N}", null) | ||
{ | ||
} | ||
|
||
public Context(HttpContext context) : this(context.TryGetCorrelationId(), context.TraceIdentifier, | ||
new IdentityContext(context.User), context.GetUserIpAddress(), | ||
context.Request.Headers["user-agent"]) | ||
Check warning on line 21 in src/Shared/StellarChat.Shared.Infrastructure/Contexts/Context.cs GitHub Actions / build
|
||
{ | ||
} | ||
|
||
public Context(Guid? correlationId, string traceId, IIdentityContext identity = null, string ipAddress = null, | ||
Check warning on line 25 in src/Shared/StellarChat.Shared.Infrastructure/Contexts/Context.cs GitHub Actions / build
|
||
string userAgent = null) | ||
{ | ||
CorrelationId = correlationId ?? Guid.NewGuid(); | ||
TraceId = traceId; | ||
Identity = identity ?? IdentityContext.Empty; | ||
IpAddress = ipAddress; | ||
UserAgent = userAgent; | ||
} | ||
|
||
public static IContext Empty => new Context(); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Shared/StellarChat.Shared.Infrastructure/Contexts/ContextAccessor.cs
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,31 @@ | ||
using StellarChat.Shared.Abstractions.Contexts; | ||
|
||
namespace StellarChat.Shared.Infrastructure.Contexts; | ||
|
||
public sealed class ContextAccessor | ||
{ | ||
private static readonly AsyncLocal<ContextHolder> Holder = new(); | ||
|
||
public IContext Context | ||
{ | ||
get => Holder.Value?.Context; | ||
set | ||
{ | ||
var holder = Holder.Value; | ||
if (holder != null) | ||
{ | ||
holder.Context = null; | ||
} | ||
|
||
if (value != null) | ||
{ | ||
Holder.Value = new ContextHolder { Context = value }; | ||
} | ||
} | ||
} | ||
|
||
private class ContextHolder | ||
{ | ||
public IContext Context; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Shared/StellarChat.Shared.Infrastructure/Contexts/Extensions.cs
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 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace StellarChat.Shared.Infrastructure.Contexts; | ||
|
||
public static class Extensions | ||
{ | ||
public static IServiceCollection AddContext(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<ContextAccessor>(); | ||
services.AddTransient(sp => sp.GetRequiredService<ContextAccessor>().Context); | ||
|
||
return services; | ||
} | ||
|
||
public static IApplicationBuilder UseContext(this IApplicationBuilder app) | ||
{ | ||
app.Use((ctx, next) => | ||
{ | ||
ctx.RequestServices.GetRequiredService<ContextAccessor>().Context = new Context(ctx); | ||
|
||
return next(); | ||
}); | ||
|
||
return app; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Shared/StellarChat.Shared.Infrastructure/Contexts/IdentityContext.cs
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,43 @@ | ||
using StellarChat.Shared.Abstractions.Contexts; | ||
using System.Security.Claims; | ||
|
||
namespace StellarChat.Shared.Infrastructure.Contexts; | ||
|
||
public class IdentityContext : IIdentityContext | ||
{ | ||
public bool IsAuthenticated { get; } | ||
public Guid Id { get; } | ||
public string? Role { get; } | ||
Check warning on line 10 in src/Shared/StellarChat.Shared.Infrastructure/Contexts/IdentityContext.cs GitHub Actions / build
|
||
public Dictionary<string, IEnumerable<string>>? Claims { get; } | ||
Check warning on line 11 in src/Shared/StellarChat.Shared.Infrastructure/Contexts/IdentityContext.cs GitHub Actions / build
|
||
|
||
private IdentityContext() { } | ||
|
||
public IdentityContext(Guid? id) | ||
{ | ||
Id = id ?? Guid.Empty; | ||
IsAuthenticated = id.HasValue; | ||
Role = null; | ||
Claims = null; | ||
} | ||
|
||
public IdentityContext(ClaimsPrincipal? principal) | ||
{ | ||
if (principal?.Identity is null || string.IsNullOrWhiteSpace(principal.Identity?.Name)) | ||
{ | ||
IsAuthenticated = false; | ||
Id = Guid.Empty; | ||
Role = null; | ||
Claims = null; | ||
return; | ||
} | ||
|
||
IsAuthenticated = principal.Identity.IsAuthenticated; | ||
Id = IsAuthenticated ? Guid.Parse(principal.Identity.Name) : Guid.Empty; | ||
Role = principal.Claims.SingleOrDefault(x => x.Type == ClaimTypes.Role)?.Value; | ||
Claims = principal.Claims.GroupBy(x => x.Type) | ||
.ToDictionary(x => x.Key, x => x.Select(c => c.Value.ToString())); | ||
} | ||
|
||
public static IIdentityContext Empty => new IdentityContext(); | ||
} | ||
|
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 |
---|---|---|
@@ -1,11 +1,63 @@ | ||
using System.Text.RegularExpressions; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace StellarChat.Shared.Infrastructure; | ||
|
||
public static class Extensions | ||
{ | ||
private const string CorrelationIdKey = "correlation-id"; | ||
|
||
public static string ToSnakeCase(this string input) | ||
=> Regex.Replace( | ||
Regex.Replace( | ||
Regex.Replace(input, @"([\p{Lu}]+)([\p{Lu}][\p{Ll}])", "$1_$2"), @"([\p{Ll}\d])([\p{Lu}])", "$1_$2"), @"[-\s]", "_").ToLower(); | ||
|
||
public static bool IsEmpty(this string value) | ||
=> string.IsNullOrWhiteSpace(value); | ||
|
||
public static bool IsNotEmpty(this string value) | ||
=> !value.IsEmpty(); | ||
|
||
public static IApplicationBuilder UseCorrelationId(this IApplicationBuilder app) | ||
=> app.Use((ctx, next) => | ||
{ | ||
ctx.Items.Add(CorrelationIdKey, Guid.NewGuid()); | ||
return next(); | ||
}); | ||
|
||
public static T BindOptions<T>(this IConfiguration configuration, string sectionName) where T : new() | ||
=> BindOptions<T>(configuration.GetSection(sectionName)); | ||
|
||
public static T BindOptions<T>(this IConfigurationSection section) where T : new() | ||
{ | ||
var options = new T(); | ||
section.Bind(options); | ||
return options; | ||
} | ||
|
||
public static Guid? TryGetCorrelationId(this HttpContext context) | ||
=> context.Items.TryGetValue(CorrelationIdKey, out var id) ? (Guid?)id : null; | ||
|
||
public static string GetUserIpAddress(this HttpContext context) | ||
{ | ||
if (context is null) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var ipAddress = context.Connection.RemoteIpAddress?.ToString(); | ||
if (context.Request.Headers.TryGetValue("x-forwarded-for", out var forwardedFor)) | ||
{ | ||
var ipAddresses = forwardedFor.ToString().Split(",", StringSplitOptions.RemoveEmptyEntries); | ||
|
||
if (ipAddresses.Any()) | ||
{ | ||
ipAddress = ipAddresses[0]; | ||
} | ||
} | ||
|
||
return ipAddress ?? string.Empty; | ||
} | ||
} |
Oops, something went wrong.