chore: deploy initial code base
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Strata.Id.Client.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Strata.RxNorm.Biz.Utilities
|
||||
{
|
||||
public static class LogUtilsExtensions
|
||||
{
|
||||
internal static readonly string Microservice = "strata-rxnorm-";
|
||||
internal static readonly string[] strings = ["password"];
|
||||
|
||||
public static Dictionary<string, TValue> ToDictionary<TValue>(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(obj);
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
MaxDepth = 0
|
||||
};
|
||||
return JsonSerializer.Deserialize<Dictionary<string, TValue>>(json, options);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new Dictionary<string, TValue> { { "object", (TValue)obj } };
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<string, object> DatabaseDictionary(this StrataIdFullDto database, string service)
|
||||
{
|
||||
var dictionary = new Dictionary<string, object> { { "service", $"{Microservice}{service}" } };
|
||||
if (database != null)
|
||||
{
|
||||
dictionary.Add("db", new Dictionary<string, object> {
|
||||
{ "guid", database.DatabaseGUID },
|
||||
{ "instance", database.PhysicalName },
|
||||
{ "server", database.ServerName },
|
||||
{ "strata_id", database.StrataId },
|
||||
{ "org_pin", database.OrgPin },
|
||||
{ "name", database.DatabaseName }
|
||||
});
|
||||
}
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// This turns the dbContext connction string into a dictionary of elements.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// It splits the string by semi-colons, then splits each of those by the equal symbol.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The left side is the dictionary key, the right side is the dictionary value, ignoring the Password (if present).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="dbContext"></param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<string, object> DbContextDictionary(this DatabaseFacade dbContext, string service)
|
||||
{
|
||||
var dictionary = dbContext?.GetConnectionString().Split(';')
|
||||
.Select(x => x.Split('='))
|
||||
.Select(y => new { key = y[0].Replace(" ", ""), value = y[1] })
|
||||
.Where(x => !strings.Contains(x.key.ToLowerInvariant()))
|
||||
.ToDictionary(x => x.key, x => x.value) ?? [];
|
||||
return new Dictionary<string, object> { { "service", $"{Microservice}{service}" }, { "dbContext", dictionary } };
|
||||
}
|
||||
|
||||
public static void LogServiceError<T>(this ILogger<T> logger, Exception exception, DbContext dbContext = null, string message = null, Dictionary<string, object> info = null)
|
||||
{
|
||||
if (logger == null) { return; }
|
||||
if (exception.InnerException != null)
|
||||
{
|
||||
logger.LogServiceError(exception.InnerException, dbContext, message, info);
|
||||
return;
|
||||
}
|
||||
var dictionary = dbContext?.Database.DbContextDictionary(typeof(T).Name)
|
||||
?? new Dictionary<string, object> { { "service", $"{Microservice}{typeof(T).Name}" } };
|
||||
if (info != null)
|
||||
{
|
||||
dictionary.Add("information", info);
|
||||
}
|
||||
var msg = !string.IsNullOrEmpty(message)
|
||||
? message
|
||||
: exception.InnerException?.Message ?? exception.Message;
|
||||
using (logger.BeginScope(dictionary))
|
||||
logger.LogError(exception.InnerException ?? exception, "{msg}", msg);
|
||||
}
|
||||
|
||||
public static void LogServiceInfo<T>(this ILogger<T> logger, DbContext dbContext = null, string message = null, Dictionary<string, object> info = null)
|
||||
{
|
||||
if (logger == null) { return; }
|
||||
var dictionary = dbContext?.Database.DbContextDictionary(typeof(T).Name)
|
||||
?? new Dictionary<string, object> { { "service", $"{Microservice}{typeof(T).Name}" } };
|
||||
if (info != null)
|
||||
{
|
||||
dictionary.Add("information", info);
|
||||
}
|
||||
var msg = !string.IsNullOrEmpty(message) ? message : typeof(T).Name;
|
||||
using (logger.BeginScope(dictionary))
|
||||
logger.LogInformation("{msg}", msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user