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 ToDictionary(this object obj) { try { var json = JsonSerializer.Serialize(obj); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, MaxDepth = 0 }; return JsonSerializer.Deserialize>(json, options); } catch { return new Dictionary { { "object", (TValue)obj } }; } } public static Dictionary DatabaseDictionary(this StrataIdFullDto database, string service) { var dictionary = new Dictionary { { "service", $"{Microservice}{service}" } }; if (database != null) { dictionary.Add("db", new Dictionary { { "guid", database.DatabaseGUID }, { "instance", database.PhysicalName }, { "server", database.ServerName }, { "strata_id", database.StrataId }, { "org_pin", database.OrgPin }, { "name", database.DatabaseName } }); } return dictionary; } /// /// /// This turns the dbContext connction string into a dictionary of elements. /// /// /// It splits the string by semi-colons, then splits each of those by the equal symbol. /// /// /// The left side is the dictionary key, the right side is the dictionary value, ignoring the Password (if present). /// /// /// /// public static Dictionary 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 { { "service", $"{Microservice}{service}" }, { "dbContext", dictionary } }; } public static void LogServiceError(this ILogger logger, Exception exception, DbContext dbContext = null, string message = null, Dictionary 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 { { "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(this ILogger logger, DbContext dbContext = null, string message = null, Dictionary info = null) { if (logger == null) { return; } var dictionary = dbContext?.Database.DbContextDictionary(typeof(T).Name) ?? new Dictionary { { "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); } } }