internal static void Log(WSLogRecord log) { try { HttpContext context = HttpContext.Current; if (log != null && log.Any() && context != null) { queue.Enqueue(log); WriteLog(context); } } catch (Exception) { } }
private static void WriteLog(HttpContext context) { if (state == LogState.Ready) { try { state = LogState.InProgress; while (queue.Count > 0) { try { WSLogRecord log = queue.Dequeue(); DirectoryInfo dirTo = new DirectoryInfo(WSServerMeta.MapPath(WSConstants.LINKS.LogPath)); if (!Directory.Exists(dirTo.FullName)) { Directory.CreateDirectory(dirTo.FullName); } if (Directory.Exists(dirTo.FullName)) { FileInfo logFile = new FileInfo($"{dirTo}\\{(log.IsError ? "error_" : "")}{ DateTime.Now.ToString("yyyy_MM_dd")}.log"); if (logFile.Exists) { logFile.IsReadOnly = false; } using (TextWriter writer = new StreamWriter(logFile.FullName, logFile.Exists)) { writer.WriteLine($""); writer.WriteLine($"___________{log.date.ToString(WSConstants.DATE_FORMAT)} => New log created [{log.Name}] by [" + (context.Request != null ? context.Request.UserHostAddress : "0.0.0.0") + "]:____________"); foreach (string line in log) { writer.WriteLine("\t-\t" + line); } writer.WriteLine($"____________log end._________________________________________________________________"); writer.WriteLine($""); } logFile = new FileInfo(logFile.FullName); if (logFile.Exists) { logFile.IsReadOnly = true; } } } finally { } } } finally { state = LogState.Ready; } } }
private static bool resetSchema(FileInfo _SRC_CONFIG, WSSources <WSTableSource> _SOURCES) { WSLogRecord Log = new WSLogRecord("RESET SCHEMA"); Log.Add($"RESET SCHEMA START"); Log.Add($"Original schema:[{(_SRC_CONFIG != null ? ("Path:" + _SRC_CONFIG.FullName + ", Exists:" + _SRC_CONFIG.Exists) : "null")}]"); Log.Add($"WSSources:[{((_SOURCES != null && _SOURCES.Any()) ? _SOURCES.Select(s => s.NAME).Aggregate((a, b) => a + "," + b) : "none")}]"); bool done = false; try { if (_SOURCES != null && _SRC_CONFIG != null) { if (!File.Exists(_SRC_CONFIG.FullName) || saveArchive(_SRC_CONFIG, ref Log)) { if (File.Exists(_SRC_CONFIG.FullName)) { _SRC_CONFIG.IsReadOnly = false; Log.Add($"Original schema:[{_SRC_CONFIG.FullName}] set 'ReadOnly' attribute to:[{_SRC_CONFIG.IsReadOnly}]"); } string orgPath = _SRC_CONFIG.FullName; string tempPath = orgPath + ".temp"; using (TextWriter writer = new StreamWriter(tempPath)) { new XmlSerializer(typeof(WSSources <WSTableSource>)).Serialize(writer, _SOURCES); Log.Add($"Temp schema:[{tempPath}] created"); File.Delete(orgPath); Log.Add($"Original schema:[{orgPath}] deleted"); File.Copy(tempPath, orgPath); Log.Add($"Temp schema:[{tempPath}] copied to [{orgPath}]"); _SRC_CONFIG = new FileInfo(orgPath); Log.Add($"Original schema {(_SRC_CONFIG.Exists ? "recreated" : "FAILED recreate")}"); } if (_SRC_CONFIG != null && File.Exists(_SRC_CONFIG.FullName)) { File.Delete(tempPath); Log.Add($"Temp schema {(!File.Exists(tempPath) ? "deleted" : "FAILED to delete")}"); done = true; } } } } catch (Exception e) { Log.Add($"\n-----------------------Exception ------------------------"); Log.Add($"Message:\n{e.Message}"); Log.Add($"StackTrace:\n{e.StackTrace}"); Log.Add($"-----------------------Exception END -------------------\n"); WSStatus status = WSStatus.NONE.clone(); LogError(typeof(WSServerMeta), e, ref status); } ReloadXMLSet[_SRC_CONFIG.Name] = (_SRC_CONFIG != null && File.Exists(_SRC_CONFIG.FullName)) ? _SRC_CONFIG.LastWriteTime.Ticks : ReloadXMLSet[_SRC_CONFIG.Name]; Log.Add($"Schema 'LastModified' set to : [{ReloadXMLSet[_SRC_CONFIG.Name].ToString(WSConstants.DATE_FORMAT)}]"); Log.Add($"RESET SCHEMA DONE"); Log.Save(); return(done); }