mudbus_master_csharp project added directly to the solution
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
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
|
||||
/// <summary>
|
||||
/// init standard program logger
|
||||
/// </summary>
|
||||
/// <param name="stdLogFolderPath">use a custom log folder or null if use default</param>
|
||||
/// <param name="filename">set a custom log file name without extension (ex. "UserCustomLogFile") or null if use default</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Automated get function name for the function calling this
|
||||
/// Used for automating getting a function name to write in a log
|
||||
/// </summary>
|
||||
/// <param name="inclMsg">Add extra message to log with function name </param>
|
||||
/// <returns>"[CALLING_FUNCTION_NAME]() -> [inclMsg]"</returns>
|
||||
[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;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a time stamp in program log file.. used to time differend code / controls load time
|
||||
/// </summary>
|
||||
/// <param name="processObj"> class or window / control obkject to make time stamp for</param>
|
||||
/// <param name="desc">description text</param>
|
||||
/// <param name="forceLog">Force making log and ignore DoTimeStamp setting</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add to program log
|
||||
/// </summary>
|
||||
/// <param name="ex">Exception to log</param>
|
||||
/// <param name="isError">Is an error or just as log</param>
|
||||
/// <param name="className">what class or where function is located in</param>
|
||||
/// <param name="functionName">what function it was in like "myFunc()" and null if not used</param>
|
||||
/// <param name="dataInfo">Additional information also to include like "while doing x and y..."</param>
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user