using Microsoft.Extensions.Configuration; using System.Collections.Concurrent; namespace Kit.Helpers.Log { public enum TypeInfo { Debug = 1, Info, Warn, Error, } public class LoggerKey : IDisposable { private bool disposed = false; public Guid Key { get; set; } public void Dispose() { Dispose(true); // подавляем финализацию GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { LoggerDB.Commit(this); } // освобождаем неуправляемые объекты disposed = true; } } // Деструктор ~LoggerKey() { Dispose(false); } } public class LoggerItems { public string SessionKey { get; set; } public LogMessageType LogMessageType { get; set; } public DateTime DateCreated { get; set; } public string Target { get; set; } public string Action { get; set; } public string Message { get; set; } public double Duration { get; set; } } public static class LoggerDB { private static string _connectionString; private static bool _isRun; private static ConcurrentDictionary _items = new ConcurrentDictionary(); public static void Configure(IConfiguration configuration) { _connectionString = "";// new ConfigurationHelper("Logger") ConfigurationManager.ConnectionStrings["Logger.Db"].ConnectionString; var config = new ConfigurationHelper(configuration, "LogDb"); _isRun = config.GetAppSettingValue("IsRun").TryParseToBool(false); } public static LoggerKey Log(string sessionKey, LogMessageType type, string target, string action, string message) { return Log(sessionKey, type, DateTime.UtcNow, target, action, message); } public static LoggerKey Log(string sessionKey, LogMessageType type, DateTime dateCreated, string target, string action, string message) { if (_isRun) { Guid key = Guid.NewGuid(); _items.TryAdd(key, new LoggerItems { SessionKey = sessionKey, Action = action, DateCreated = dateCreated, Duration = 0, LogMessageType = type, Message = message, Target = target }); return new LoggerKey { Key = key }; } return new LoggerKey { Key = Guid.Empty }; ; } public static void Commit(LoggerKey key, DateTime? finishDate = null) { if (_items.ContainsKey(key.Key)) { LoggerItems item = _items.GetByKey(key.Key); double duration = ((finishDate ?? DateTime.UtcNow) - item.DateCreated).TotalMilliseconds; //_connectionString.ExecuteScalar("[Logs].[Insert_Logger]", true, new List> //{ // new KeyValuePair("@LogMessageType", (int)item.LogMessageType), // new KeyValuePair("@SessionKey", item.SessionKey), // new KeyValuePair("@DateCreate", item.DateCreated), // new KeyValuePair("@Target", item.Target), // new KeyValuePair("@Action", item.Action), // new KeyValuePair("@Message", item.Message), // new KeyValuePair("@Duration", duration), //}); _items.TryRemove(key.Key, out item!); } } public static void Commit(params LoggerKey[] keys) { foreach (LoggerKey key in keys) { Commit(key); } } public static void Commit(string sessionKey, LogMessageType type, string target, string action, DateTime dateTime, Dictionary logs) { foreach (var log in logs) { //_connectionString.ExecuteScalar("[Logs].[Insert_Logger]", true, new List> //{ // new KeyValuePair("@LogMessageType", (int)type), // new KeyValuePair("@SessionKey", sessionKey), // new KeyValuePair("@DateCreate", dateTime), // new KeyValuePair("@Target", target), // new KeyValuePair("@Action", action), // new KeyValuePair("@Message", log.Key), // new KeyValuePair("@Duration", log.Value), //}); } } } }