Files
LM-Middleware/HexcalMC/Base/DebugDfn.cs
T
zhengxuan.zhang 08daf2ced4 格式化代码
2024-02-21 17:05:21 +08:00

130 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HexcalMC.Base
{
public class DebugDfn
{
public static string StrDebugFile = Application.StartupPath + "\\File\\Debug.txt";
public static string StrDebugSavePath = Application.StartupPath + "\\File\\DebugFiles";
public static string StrDebugFileTemp = Application.StartupPath + "\\File\\DebugTemp.txt"; //临时存储,用于菜单查看
static string _strStartTime = ""; //程序启动时间
static string _strEndTime = ""; //程序关闭时间
public static RichTextBox TextBoxMsg;
//=================================================================
public static void StartDebugObj()
{
System.IO.TextWriter log = new System.IO.StreamWriter(DebugDfn.StrDebugFile);
TextWriterTraceListener logger = new TextWriterTraceListener(log);
Trace.Listeners.Add(logger);
_strStartTime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
}
public static void SaveDebugFile()
{
AddLog("软件关闭!");
_strEndTime = DateTime.Now.ToString("yyyy.MM.dd HH-mm-ss");
string copyFileName = StrDebugSavePath + "\\DebugFiles\\" + "Debug(" + _strStartTime + " To " +
_strEndTime + ")" + ".txt";
if (File.Exists(StrDebugFile))
File.Copy(StrDebugFile, copyFileName, true);
}
//=================================================================
#region
public static void AddLog(string str)
{
MyBase.TraceWriteLine(str);
}
public static void AddLogText(string str, Color mColor = new Color())
{
MyBase.TraceWriteLine(str);
try
{
TextBoxMsg.BeginInvoke((EventHandler)delegate
{
Color setColor = Color.White;
if (mColor == new Color())
{
if (str.ToUpper().Contains("ERROR") || str.ToUpper().Contains("错误") ||
str.ToUpper().Contains("出错") || str.ToUpper().Contains("EXCEPTION") ||
str.ToUpper().Contains("异常") || str.ToUpper().Contains("失败"))
{
setColor = Color.Red;
}
else if (str.ToUpper().Contains("WARNING") || str.ToUpper().Contains("警告"))
{
setColor = Color.DarkOrange;
}
}
else
{
setColor = mColor;
}
string strText = DateTime.Now.ToString("HH:mm:ss.ff") + "--" + str + Environment.NewLine;
TextBoxMsg.SelectionStart = TextBoxMsg.TextLength;
if (string.IsNullOrEmpty(str))
SetText(TextBoxMsg, str, setColor, false, 16);
else
SetText(TextBoxMsg, strText, setColor, false, 16);
if (TextBoxMsg.Lines.Length > 800)
{
TextBoxMsg.Select(0, TextBoxMsg.TextLength / 2);
TextBoxMsg.Cut();
}
TextBoxMsg.ScrollToCaret();
});
}
catch
{
}
}
public static void SetText(RichTextBox mRichTextBox, string strText, Color mColor, bool bBold = false,
float size = 16)
{
mRichTextBox.Invoke(((EventHandler)delegate
{
SetFont(mRichTextBox, mColor, bBold, size);
mRichTextBox.SelectedText = strText;
}));
}
public static void SetFont(RichTextBox mRichTextBox, Color mColor, bool bBold = false, float size = 16)
{
mRichTextBox.SelectionColor = mColor;
if (bBold)
mRichTextBox.SelectionFont = new System.Drawing.Font("Segoe UI", size, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));
else
mRichTextBox.SelectionFont = new System.Drawing.Font("Segoe UI", size,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));
}
public static void SetErrorColor(Color inColor)
{
if (TextBoxMsg != null)
{
TextBoxMsg.BeginInvoke((EventHandler)delegate { TextBoxMsg.BackColor = inColor; });
}
}
#endregion
}
}