public void OnGet() { try { if (!_service.EnvAllowed() || !_service.IsLogged()) { GoToIndex(); } else { Detail_Log = _service.Get((LogSource)Detail_Source, Detail_ID, Detail_Level).Result; } } catch (Exception e) { var item = new Log_Master() { Message = e.Message, DateTime = DateTime.Now, LevelID = (int)LogLevel.Error, CallerMemberName = "DetailModel.OnGet", FullData = e.ToString() }; _service.LogError(item, e); } }
public Task Log(Log_Master data) { string file = string.Empty; switch ((LogLevel)data.LevelID) { case LogLevel.Error: file = Path.Combine(_errorFolder, $"{DateTime.Today:yyyy-MM-dd}_{LogLevel.Error}.txt"); break; case LogLevel.Warning: file = Path.Combine(_warningFolder, $"{DateTime.Today:yyyy-MM-dd}_{LogLevel.Warning}.txt"); break; case LogLevel.Information: file = Path.Combine(_infoFolder, $"{DateTime.Today:yyyy-MM-dd}_{LogLevel.Information}.txt"); break; default: case LogLevel.Trace: file = Path.Combine(_traceFolder, $"{DateTime.Today:yyyy-MM-dd}_{LogLevel.Trace}.txt"); break; } var lines = new List <string>(); lines.Add($"[START][{nameof(Log_Master.ID)}:{data.DateTime.Ticks}]"); lines.Add($"[{nameof(Log_Master.DateTime)}:{data.DateTime:yyyy-MM-dd HH:mm:ss}][{((LogLevel)data.LevelID)}][{nameof(Log_Master.CallerMemberName)}:{data.CallerMemberName}][{nameof(Log_Master.CallerMemberLineNumber)}:{data.CallerMemberLineNumber}]"); lines.Add($"[{nameof(Log_Master.Message)}:{data.Message}]"); if (!string.IsNullOrWhiteSpace(data.FullData)) { lines.Add($"[{nameof(Log_Master.FullData)}:{data.FullData}]"); } lines.Add("[END]"); for (int i = 0; i < 30; i++) { try { File.AppendAllLines(file, lines); break; } catch { Thread.Sleep(100); } } return(Task.CompletedTask); }
public Task LogInformation(string message, string data = null, [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0) { var entity = new Log_Master() { DateTime = DateTime.Now, LevelID = (int)LogLevel.Information, CallerMemberName = caller, CallerMemberLineNumber = line, Message = message, FullData = data }; if (_configuration.UseDailyLogFile) { using (var ctx = new ContextFile(_configuration.File_FolderPath)) ctx.Log(entity); } if (_configuration.UseSQLite) { using (var ctx = new ContextSQLite(_configuration.SQLite_FullPath)) { ctx.Logger_Masters.Add(entity); ctx.SaveEntitiesAsync(); } } if (_configuration.UseSQL) { using (var ctx = new ContextSQL(_configuration.SQL_ConnectionString)) { entity.ID = 0; ctx.Logger_Masters.Add(entity); ctx.SaveEntitiesAsync(); } } return(Task.CompletedTask); }
public Task <Log_Master> Get(LogLevel level, long id) { Log_Master entity = null; return(Task.FromResult(entity)); }
public Task <IEnumerable <Log_Master> > GetList(int?level = null, DateTime?from = null, DateTime?to = null) { var fileNames = new List <string>(); if (!level.HasValue || level.Value == (int)LogLevel.Error) { fileNames.AddRange(Directory .GetFiles(_errorFolder) ?.Where(t => !string.IsNullOrEmpty(t) && Path.GetExtension(t) == ".txt" && t.Contains($"_{LogLevel.Error}"))); } if (!level.HasValue || level.Value == (int)LogLevel.Warning) { fileNames.AddRange(Directory .GetFiles(_warningFolder) ?.Where(t => !string.IsNullOrEmpty(t) && Path.GetExtension(t) == ".txt" && t.Contains($"_{LogLevel.Warning}"))); } if (!level.HasValue || level.Value == (int)LogLevel.Information) { fileNames.AddRange(Directory .GetFiles(_infoFolder) ?.Where(t => !string.IsNullOrEmpty(t) && Path.GetExtension(t) == ".txt" && t.Contains($"_{LogLevel.Information}"))); } if (!level.HasValue || level.Value == (int)LogLevel.Trace) { fileNames.AddRange(Directory .GetFiles(_traceFolder) ?.Where(t => !string.IsNullOrEmpty(t) && Path.GetExtension(t) == ".txt" && t.Contains($"_{LogLevel.Trace}"))); } if (from.HasValue) { fileNames = fileNames ?.Where(t => DateTime.TryParse(Path.GetFileNameWithoutExtension(t).Split('_').FirstOrDefault(), out DateTime dateTime) ? from.Value <= dateTime : false) ?.ToList(); } if (to.HasValue) { fileNames = fileNames ?.Where(t => DateTime.TryParse(Path.GetFileNameWithoutExtension(t).Split('_').FirstOrDefault(), out DateTime dateTime) ? to.Value.AddDays(1).Date > dateTime : false) ?.ToList(); } var fileContents = new List <string>(); foreach (var f in fileNames) { fileContents.AddRange(File.ReadAllLines(f)?.Where(t => !string.IsNullOrWhiteSpace(t))); } var entities = new List <Log_Master>(); Log_Master entity = null; bool isFullData = false; foreach (var elm in fileContents) { if (elm.StartsWith("[START]")) { isFullData = false; entity = new Log_Master(); } else if (elm.StartsWith($"[{nameof(Log_Master.DateTime)}")) { isFullData = false; var datas = elm.Split(new string[] { "][" }, StringSplitOptions.RemoveEmptyEntries); foreach (var data in datas) { if (data.StartsWith($"[{nameof(Log_Master.DateTime)}")) { var dres = DateTime.TryParse(data.Replace($"[{nameof(Log_Master.DateTime)}:", "").Trim(), out DateTime d); if (dres) { entity.DateTime = d; } } else if (data.StartsWith($"{LogLevel.Error}")) { entity.LevelID = (int)LogLevel.Error; } else if (data.StartsWith($"{LogLevel.Warning}")) { entity.LevelID = (int)LogLevel.Warning; } else if (data.StartsWith($"{LogLevel.Information}")) { entity.LevelID = (int)LogLevel.Information; } else if (data.StartsWith($"{LogLevel.Trace}")) { entity.LevelID = (int)LogLevel.Trace; } else if (data.StartsWith($"{nameof(Log_Master.CallerMemberName)}")) { entity.CallerMemberName = data.Replace($"[{nameof(Log_Master.CallerMemberName)}:", "").Trim(); } else if (data.StartsWith($"{nameof(Log_Master.CallerMemberLineNumber)}")) { var dres = int.TryParse(data.Replace($"[{nameof(Log_Master.CallerMemberName)}:", "").Trim(), out int d); if (dres) { entity.CallerMemberLineNumber = d; } } } } else if (elm.StartsWith($"[{nameof(Log_Master.Message)}")) { isFullData = false; var data = elm.Replace($"[{nameof(Log_Master.Message)}:", "").Replace("]", ""); entity.Message = data?.Trim() ?? null; } else if (!elm.StartsWith("[END]") && (elm.StartsWith($"[{nameof(Log_Master.FullData)}") || isFullData)) { isFullData = true; var data = elm.Replace($"[{nameof(Log_Master.FullData)}:", "").Replace("]", ""); entity.FullData ??= ""; entity.FullData += data ?? null; } else if (elm.StartsWith("[END]")) { isFullData = false; entities.Add(entity); entity = null; } } return(Task.FromResult(entities.AsEnumerable())); }
public async Task LogError(Log_Master item, Exception exception = null) { await _logger.LogError(item.Message, exception, item.CallerMemberName, item.CallerMemberLineNumber); }
public async Task LogWarning(Log_Master item) { await _logger.LogWarning(item.Message, item.FullData, item.CallerMemberName, item.CallerMemberLineNumber); }
public async Task LogInfo(Log_Master item) { await _logger.LogInformation(item.Message, item.FullData, item.CallerMemberName, item.CallerMemberLineNumber); }