using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace XYLEM.Base.Files { public class ProgramLogCSVFileClass { private int _LogCountErrors = -1; private int _LogCountTotal = -1; public int LogCountErrors { get { return _LogCountErrors; } } public int LogCountTotal { get { return _LogCountTotal; } } public string StdLogFilePath = null; public event EventHandler OnErrorIsAdded; #if (!DEBUG) public bool DoTimeStamp = false; #else /*#warning DoTimeStamp is true!!!!!!!!!!!!!!!! public bool DoTimeStamp = true;*/ public bool DoTimeStamp = false; #endif /// /// init standard program logger /// /// use a custom log folder or null if use default /// set a custom log file name without extension (ex. "UserCustomLogFile") or null if use default public ProgramLogCSVFileClass(string stdLogFolderPath = null, string filename = null) { if (String.IsNullOrEmpty(filename)) { filename = $"{AppConst.ProductVersion}_program_log"; } if (!string.IsNullOrEmpty(stdLogFolderPath)) { if (!Directory.Exists(stdLogFolderPath)) { PathTools.CreateDirPath(stdLogFolderPath); } StdLogFilePath=Path.Combine(stdLogFolderPath, filename + ".txt"); } else { this.StdLogFilePath = Path.Combine(AppConst.ProgramUserFolderRootPath(), filename + ".txt"); #if (DEBUG) Console.WriteLine($"Using standard program log path:\n{this.StdLogFilePath}"); #endif } } /// /// Automated get function name for the function calling this /// Used for automating getting a function name to write in a log /// /// Add extra message to log with function name /// "[CALLING_FUNCTION_NAME]() -> [inclMsg]" [MethodImpl(MethodImplOptions.NoInlining)] public string GetCurrentMethod(string inclMsg = null) { var st = new StackTrace(); var sf = st.GetFrame(1); var nameTxt = $"{sf.GetMethod().Name}()"; if (!String.IsNullOrEmpty(inclMsg)) { nameTxt += " -> " + inclMsg; } return nameTxt; } /// /// Automated get function name for the function calling this /// Used for automating getting a function name to write in a log /// /// Add extra message to log with function name /// "[CALLING_FUNCTION_NAME]() -> [inclMsg]" [MethodImpl(MethodImplOptions.NoInlining)] public static string GetCurrentStaticMethod(string inclMsg = null) { var st = new StackTrace(); var sf = st.GetFrame(1); var nameTxt = $"{sf.GetMethod().Name}()"; if (!String.IsNullOrEmpty(inclMsg)) { nameTxt += " -> " + inclMsg; } return nameTxt; } internal void AddToLogFile(bool isError, string nameSingleLine, string infoMultibleLines) { AddToLogFile(isError, StdLogFilePath, nameSingleLine, infoMultibleLines); } internal void AddToLogFile(bool isError, string LogFilePath, string nameSingleLine, string infoMultibleLines) { try { if (isError) { _LogCountErrors++; try { if (OnErrorIsAdded != null) { OnErrorIsAdded(null, null); } } catch { } // Don't handle this if something breaks down } _LogCountTotal++; if (System.IO.File.Exists(LogFilePath)) { FileInfo f = new FileInfo(LogFilePath); if (f.Length > (DoTimeStamp ? 1000000 : 10000)) { f.Delete(); } } else { PathTools.CreateDirPathForFile(LogFilePath); } var message = infoMultibleLines; var length = message.Length; File.AppendAllText(LogFilePath, "¤¤¤ " + nameSingleLine.Trim() + " \"" + DateTime.Now.ToString() + "\" ¤¤¤ " + Environment.NewLine + message + Environment.NewLine); #if (DEBUG) System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff ") + " ¤¤¤ " + nameSingleLine.Trim() + " ¤¤¤ " + Environment.NewLine + message ); #endif } catch (Exception ex) { // Ups just to make sure that this don't result in program is closed string test = ex.ToMessageText(); } } /// /// Add a time stamp in program log file.. used to time differend code / controls load time /// /// class or window / control obkject to make time stamp for /// description text /// Force making log and ignore DoTimeStamp setting internal void AddToTimestampLog(object processObj, string desc, bool forceLog = false) { try { if (DoTimeStamp || forceLog) { AddToLogFile(false, StdLogFilePath, processObj.ToString() + DateTime.Now.ToString(" [HH:mm:ss fff]"), desc); } } catch { } } internal void AddToProgramLogFile(bool isError, string nameSingleLine, string infoMultibleLines) { AddToLogFile(isError, StdLogFilePath, nameSingleLine, infoMultibleLines); } /// /// Add to program log /// /// Exception to log /// Is an error or just as log /// what class or where function is located in /// what function it was in like "myFunc()" and null if not used /// Additional information also to include like "while doing x and y..." internal void ToLog(Exception ex, bool isError, string className, string functionName = null, string dataInfo = null) { AddToProgramLogFile(isError, LogMessageTool.ToFuncText(isError, className, functionName), (String.IsNullOrEmpty(dataInfo) ? "" : dataInfo + ((ex != null) ? Environment.NewLine : "")) // Add data info if used + ((ex != null) ? ex.ToMessageText() : "")); } internal void AddFuncLog(bool isError, string className, string functionName, string dataInfo, Exception ex) { ToLog(ex, isError, className, functionName, dataInfo); } internal void AddFuncLog(bool isError, string className, string functionName, Exception ex) { AddFuncLog(isError, className, functionName, null, ex); } internal void AddFuncLog(bool isError, string className, string functionName, string dataInfo) { AddFuncLog(isError, className, functionName, dataInfo, null); } internal void AddClassInitLog(bool isError, string className, Exception ex) { AddToProgramLogFile(isError, LogMessageTool.ToInitText(isError, className), ((ex != null) ? ex.ToMessageText() : "")); } internal void AddClassInitLog(bool isError, string className, string dataInfo, Exception ex) { AddToProgramLogFile(isError, LogMessageTool.ToInitText(isError, className, dataInfo), ((ex != null) ? ex.ToMessageText() : "")); } internal void OpenProgramLogFile() { OpenLogFile(StdLogFilePath); } internal void OpenLogFile(string LogFilePath) { try { Process.Start(LogFilePath); } catch (Exception ex) { Console.WriteLine("Error Open Log " + Environment.NewLine + ex.ToMessageText()); } } } }