第一次提交至Git
This commit is contained in:
207
UTS_Core/DebugLog/ApplicationLog.vb
Normal file
207
UTS_Core/DebugLog/ApplicationLog.vb
Normal file
@@ -0,0 +1,207 @@
|
||||
Imports System.IO
|
||||
Imports System.Text
|
||||
Imports System.Windows.Forms
|
||||
|
||||
Namespace DebugLog
|
||||
''' <summary>
|
||||
''' 应用程序日志
|
||||
''' </summary>
|
||||
Public NotInheritable Class ApplicationLog
|
||||
|
||||
''' <summary>日志文件所在父文件夹路径</summary>
|
||||
Private Shared _logPath As String = Application.StartupPath
|
||||
|
||||
''' <summary>日志文件名前缀</summary>
|
||||
Private Shared _logFilePrefix As String = Application.ProductName
|
||||
|
||||
''' <summary>日志文件所在路径</summary>
|
||||
Private Shared _logFilePath As String = $"{LogDirPath}{Path.DirectorySeparatorChar}{LogFilePrefix}_{Date.Now:yyyyMMdd}.Log"
|
||||
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 保存日志的文件夹完整路径
|
||||
''' </summary>
|
||||
Public Shared Property LogDirPath As String
|
||||
Get
|
||||
If Equals(_logPath, String.Empty) Then
|
||||
_logPath = Application.StartupPath
|
||||
End If
|
||||
Return _logPath
|
||||
End Get
|
||||
Set(value As String)
|
||||
_logPath = value
|
||||
|
||||
_logFilePath = $"{LogDirPath}{Path.DirectorySeparatorChar}{LogFilePrefix}_{Date.Now:yyyyMMdd}.Log"
|
||||
End Set
|
||||
End Property
|
||||
|
||||
''' <summary>
|
||||
''' 日志文件前缀
|
||||
''' </summary>
|
||||
Public Shared Property LogFilePrefix As String
|
||||
Get
|
||||
Return _logFilePrefix
|
||||
End Get
|
||||
Set(value As String)
|
||||
_logFilePrefix = value
|
||||
_logFilePath = $"{LogDirPath}{Path.DirectorySeparatorChar}{LogFilePrefix}_{Date.Now:yyyyMMdd}.Log"
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Public Shared ReadOnly Property LogFilePath() As String
|
||||
Get
|
||||
Return _logFilePath
|
||||
End Get
|
||||
End Property
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 写入错误信息记录日志
|
||||
''' </summary>
|
||||
''' <param name="ex"></param>
|
||||
Public Shared Sub WriteErrorLog(ex As Exception)
|
||||
Dim msg As New StringBuilder
|
||||
|
||||
msg.Append($"{ex.StackTrace} {ex.Message}")
|
||||
|
||||
WriteLog(LogTypes.Error, msg.ToString())
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 写入流程信息记录日志
|
||||
''' </summary>
|
||||
''' <param name="msg"></param>
|
||||
Public Shared Sub WriteDebugLog1(msg As String)
|
||||
WriteLog1(LogTypes.Debug.ToString, msg.ToString())
|
||||
End Sub
|
||||
Public Shared Sub WriteDebugLog(msg As String)
|
||||
WriteLog(LogTypes.Debug, msg.ToString())
|
||||
End Sub
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 写入流程信息记录日志
|
||||
''' </summary>
|
||||
''' <param name="msg"></param>
|
||||
Public Shared Sub WriteInfoLog(msg As String)
|
||||
WriteLog(LogTypes.Info, msg.ToString())
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 写入警告信息记录日志
|
||||
''' </summary>
|
||||
''' <param name="msg"></param>
|
||||
Public Shared Sub WriteWarningLog(msg As String)
|
||||
WriteLog(LogTypes.Warn, msg.ToString())
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 写入错误信息记录日志
|
||||
''' </summary>
|
||||
''' <param name="msg"></param>
|
||||
Public Shared Sub WriteErrorLog(msg As String)
|
||||
WriteLog(LogTypes.Error, msg.ToString())
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 写入数据库信息记录日志
|
||||
''' </summary>
|
||||
''' <param name="msg"></param>
|
||||
Public Shared Sub WriteFatalLog(msg As String)
|
||||
WriteLog(LogTypes.Fatal, msg.ToString())
|
||||
End Sub
|
||||
|
||||
|
||||
Private Shared ReadOnly LogLock As New Object() '日志锁,防止多线程同时写日志导致冲突
|
||||
|
||||
''' <summary>
|
||||
''' 将信息入到日志
|
||||
''' </summary>
|
||||
''' <param name="logType">日志类型</param>
|
||||
''' <param name="msg">日志内容</param>
|
||||
Public Shared Sub WriteLog(logType As String, msg As String)
|
||||
'写入记录入日志文件
|
||||
SyncLock LogLock
|
||||
Try
|
||||
Dim logString As New StringBuilder
|
||||
|
||||
logString.Append($"[{Date.Now:yyyy-MM-dd HH:mm:ss:fff}]") '日志产生时间
|
||||
|
||||
logString.Append($"[{logType,-6}]") '日志类型
|
||||
|
||||
logString.Append($"[{Process.GetCurrentProcess.Id,-6}]") '日志的进程号
|
||||
|
||||
logString.Append($"[{Threading.Thread.CurrentThread.ManagedThreadId,-4}]") '日志的线程号
|
||||
|
||||
logString.Append(msg) '日志的消息主题
|
||||
|
||||
Using sw As StreamWriter = File.AppendText($"{LogDirPath}{Path.DirectorySeparatorChar}{LogFilePrefix}_{Date.Now:yyyyMMdd}.Log")
|
||||
sw.WriteLine(logString.ToString())
|
||||
End Using
|
||||
|
||||
Catch ex As Exception
|
||||
Console.WriteLine($"Uts WriteLog Error:{ex.Message}")
|
||||
End Try
|
||||
|
||||
End SyncLock
|
||||
End Sub
|
||||
Public Shared Sub WriteLog1(logType As String, msg As String)
|
||||
'写入记录入日志文件
|
||||
SyncLock LogLock
|
||||
Try
|
||||
Dim logString As New StringBuilder
|
||||
|
||||
logString.Append($"[{Date.Now:yyyy-MM-dd HH:mm:ss:fff}]") '日志产生时间
|
||||
|
||||
logString.Append($"[{logType,-6}]") '日志类型
|
||||
|
||||
logString.Append($"[{Process.GetCurrentProcess.Id,-6}]") '日志的进程号
|
||||
|
||||
logString.Append($"[{Threading.Thread.CurrentThread.ManagedThreadId,-4}]") '日志的线程号
|
||||
|
||||
logString.Append(msg) '日志的消息主题
|
||||
|
||||
Using sw As StreamWriter = File.AppendText($"{LogDirPath}{Path.DirectorySeparatorChar}{logType}_{Date.Now:yyyyMMdd}.Log")
|
||||
sw.WriteLine(logString.ToString())
|
||||
End Using
|
||||
|
||||
Catch ex As Exception
|
||||
Console.WriteLine($"Uts WriteLog Error:{ex.Message}")
|
||||
End Try
|
||||
|
||||
End SyncLock
|
||||
End Sub
|
||||
''' <summary>
|
||||
''' 写日志
|
||||
''' </summary>
|
||||
Public Shared Sub WriteLog(type As LogTypes, ByVal msg As String)
|
||||
WriteLog(type.ToString(), msg)
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 日志类型
|
||||
''' </summary>
|
||||
Public Enum LogTypes
|
||||
''' <summary>调试信息</summary>
|
||||
Debug
|
||||
|
||||
''' <summary>系统运行信息</summary>
|
||||
Info
|
||||
|
||||
''' <summary>警告信息</summary>
|
||||
Warn
|
||||
|
||||
''' <summary>错误信息应该包含对象名、发生错误点所在的方法名称、具体错误信息</summary>
|
||||
[Error]
|
||||
|
||||
''' <summary>致命信息</summary>
|
||||
Fatal
|
||||
End Enum
|
||||
End Class
|
||||
End Namespace
|
||||
95
UTS_Core/DebugLog/ControlRecord.vb
Normal file
95
UTS_Core/DebugLog/ControlRecord.vb
Normal file
@@ -0,0 +1,95 @@
|
||||
Imports System.Windows.Forms
|
||||
|
||||
Namespace DebugLog
|
||||
''' <summary>
|
||||
''' 考虑修改为自定义控件
|
||||
''' </summary>
|
||||
Public Class ControlRecord
|
||||
|
||||
Sub New(recordControl As RichTextBox)
|
||||
ShowRecord = True
|
||||
SuspendLayout = False
|
||||
MaxRecordCount = 512
|
||||
RtxRecord = recordControl
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 是否在添加内容时先挂起布局
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Property SuspendLayout() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否添加记录到控件
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Property ShowRecord() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 控件记录最大行数
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Property MaxRecordCount() As Integer
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 需要被添加数据记录的控件句柄
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Property RtxRecord() As RichTextBox
|
||||
|
||||
''' <summary>
|
||||
''' 清空内容
|
||||
''' </summary>
|
||||
Public Sub Clear()
|
||||
If RtxRecord Is Nothing Then Return
|
||||
RtxRecord.Clear()
|
||||
End Sub
|
||||
|
||||
Public Sub AppendRecord(logString As String)
|
||||
If ShowRecord = False Then Return
|
||||
If RtxRecord Is Nothing Then Return
|
||||
|
||||
If RtxRecord.InvokeRequired Then '判断是否需要开委托
|
||||
RtxRecord.Invoke(New Action(Of String)(AddressOf AppendRecord), New Object() {logString})
|
||||
Return
|
||||
End If
|
||||
|
||||
With RtxRecord
|
||||
If SuspendLayout Then
|
||||
.SuspendLayout()
|
||||
|
||||
If .Lines.Length > MaxRecordCount Then '超过上限则移除最初行内容
|
||||
.ReadOnly = False
|
||||
.SelectionStart = 0
|
||||
.SelectionLength = .GetFirstCharIndexFromLine(1)
|
||||
.SelectedText = String.Empty
|
||||
.ReadOnly = True
|
||||
.SelectionStart = .TextLength
|
||||
End If
|
||||
|
||||
.AppendText($"{Now:yyyy:MM:dd HH:mm:ss} - {logString}{vbNewLine}")
|
||||
|
||||
.ScrollToCaret()
|
||||
|
||||
.ResumeLayout(False)
|
||||
Else
|
||||
If .Lines.Length > MaxRecordCount Then '超过上限则移除最初行内容
|
||||
.ReadOnly = False
|
||||
.SelectionStart = 0
|
||||
.SelectionLength = .GetFirstCharIndexFromLine(1)
|
||||
.SelectedText = String.Empty
|
||||
.ReadOnly = True
|
||||
.SelectionStart = .TextLength
|
||||
End If
|
||||
|
||||
.AppendText($"{Now:yyyy:MM:dd HH:mm:ss} - {logString}{vbNewLine}")
|
||||
|
||||
.ScrollToCaret()
|
||||
|
||||
End If
|
||||
End With
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
End Namespace
|
||||
94
UTS_Core/DebugLog/CsDataLog.vb
Normal file
94
UTS_Core/DebugLog/CsDataLog.vb
Normal file
@@ -0,0 +1,94 @@
|
||||
Imports System.IO
|
||||
''' <summary>
|
||||
''' 日志类
|
||||
''' </summary>
|
||||
Public NotInheritable Class CsDataLog
|
||||
''' <summary>
|
||||
''' 日志类型
|
||||
''' </summary>
|
||||
Public Enum LogType
|
||||
''' <summary>堆栈跟踪信息</summary>
|
||||
Trace
|
||||
''' <summary>警告信息</summary>
|
||||
Warning
|
||||
''' <summary>错误信息应该包含对象名、发生错误点所在的方法名称、具体错误信息</summary>
|
||||
[Error]
|
||||
''' <summary>与数据库相关的信息</summary>
|
||||
SQL
|
||||
End Enum
|
||||
|
||||
|
||||
''' <summary>日志文件所在路径</summary>
|
||||
Private Shared _logPath = String.Empty
|
||||
|
||||
''' <summary>日志前缀说明信息</summary>
|
||||
Private Shared _logFilePrefix = String.Empty
|
||||
|
||||
''' <summary>
|
||||
''' 保存日志的文件夹
|
||||
''' </summary>
|
||||
Public Shared Property LogPath As String
|
||||
Get
|
||||
|
||||
If Equals(_logPath, String.Empty) Then
|
||||
_logPath = Application.StartupPath
|
||||
End If
|
||||
Return _logPath
|
||||
End Get
|
||||
Set(ByVal value As String)
|
||||
_logPath = value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
''' <summary>
|
||||
''' 日志文件前缀
|
||||
''' </summary>
|
||||
Public Shared Property LogFilePrefix As String
|
||||
Get
|
||||
Return _logFilePrefix
|
||||
End Get
|
||||
Set(ByVal value As String)
|
||||
_logFilePrefix = value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
''' <summary>
|
||||
'''
|
||||
''' </summary>
|
||||
''' <param name="ex"></param>
|
||||
Public Shared Sub WriteErrorLog(ex As Exception)
|
||||
Dim msg As String = String.Empty
|
||||
msg &= $"ErrorMessage:{ex.Message}{vbNewLine}"
|
||||
msg &= $"ErrorTime:{Now}{vbNewLine}"
|
||||
msg &= $"ErrorSource:{ex.Source}{vbNewLine}"
|
||||
msg &= $"ErrorType:{ex.GetType}{vbNewLine}"
|
||||
msg &= $"ErrorTargetSite:{ex.TargetSite}{vbNewLine}"
|
||||
msg &= $"ErrorStackTrace:{ex.StackTrace}{vbNewLine}"
|
||||
WriteLog(LogType.Error, msg)
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 将信息入到日志
|
||||
''' </summary>
|
||||
''' <param name="logType">日志类型</param>
|
||||
''' <param name="msg">日志内容</param>
|
||||
Public Shared Sub WriteLog(ByVal logType As String, ByVal msg As String)
|
||||
Dim sw As StreamWriter = Nothing
|
||||
Try
|
||||
'同一天同一类日志以追加形式保存
|
||||
sw = File.AppendText($"{LogPath}{Path.DirectorySeparatorChar}{LogFilePrefix}_{Date.Now:yyyyMMdd}.Log")
|
||||
sw.WriteLine(logType & "#" & Date.Now.ToString("yyyy-MM-dd HH:mm:ss: ") & msg)
|
||||
Catch
|
||||
Finally
|
||||
sw.Close()
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 写日志
|
||||
''' </summary>
|
||||
Public Shared Sub WriteLog(type As LogType, ByVal msg As String)
|
||||
WriteLog(type.ToString(), msg)
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
60
UTS_Core/DebugLog/CsDebugPrint.vb
Normal file
60
UTS_Core/DebugLog/CsDebugPrint.vb
Normal file
@@ -0,0 +1,60 @@
|
||||
Public NotInheritable Class CsDebugPrint
|
||||
Enum EnDebugType
|
||||
System
|
||||
ComPort
|
||||
Network
|
||||
Ftp
|
||||
End Enum
|
||||
|
||||
''' <summary>是否需要打印调试信息</summary>
|
||||
Public Shared Property IsDebug As Boolean = True
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 打印调试信息
|
||||
''' </summary>
|
||||
''' <param name="message">需要打印的信息</param>
|
||||
Public Shared Sub DebugPrint(message As String)
|
||||
If IsDebug Then
|
||||
Console.WriteLine(message)
|
||||
End If
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 打印调试信息
|
||||
''' </summary>
|
||||
''' <param name="type">打印信息类型</param>
|
||||
''' <param name="msg">需要打印的信息</param>
|
||||
Public Shared Sub DebugPrint(type As String, msg As String)
|
||||
DebugPrint($"[{type}]{msg}")
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 打印调试信息
|
||||
''' </summary>
|
||||
''' <param name="type">打印信息类型</param>
|
||||
''' <param name="msg">需要打印的信息</param>
|
||||
Public Shared Sub DebugPrint(type As EnDebugType, msg As String)
|
||||
DebugPrint(type.ToString, msg)
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 打印调试信息
|
||||
''' </summary>
|
||||
''' <param name="type">打印信息类型</param>
|
||||
''' <param name="tip">需要打印信息的提示前缀</param>
|
||||
''' <param name="msg">需要打印的信息</param>
|
||||
Public Shared Sub DebugPrint(type As String, tip As String, msg As String)
|
||||
DebugPrint(type, $"{tip}:{msg}")
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 打印调试信息
|
||||
''' </summary>
|
||||
''' <param name="type">打印信息类型</param>
|
||||
''' <param name="tip">需要打印信息的提示前缀</param>
|
||||
''' <param name="msg">需要打印的信息</param>
|
||||
Public Shared Sub DebugPrint(type As EnDebugType, tip As String, msg As String)
|
||||
DebugPrint(type.ToString, $"{tip}:{msg}")
|
||||
End Sub
|
||||
End Class
|
||||
119
UTS_Core/DebugLog/DebugPrint.vb
Normal file
119
UTS_Core/DebugLog/DebugPrint.vb
Normal file
@@ -0,0 +1,119 @@
|
||||
Imports System.Text
|
||||
|
||||
Namespace DebugLog
|
||||
Public Class DebugPrintClass
|
||||
Enum DebugPrintType
|
||||
System
|
||||
Comport
|
||||
Database
|
||||
NetWork
|
||||
Ftp
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 显示所有数据,优先度最高
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowAllData() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否显示系统信息
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowSystemData() As Byte = 1
|
||||
|
||||
''' <summary>
|
||||
''' 是否显示串口信息
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowComportData() As Byte = 1
|
||||
|
||||
''' <summary>
|
||||
''' 是否显示数据库信息
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowDatabaseData() As Byte = 1
|
||||
|
||||
''' <summary>
|
||||
''' 是否显示网络信息
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowNetWorkData() As Byte = 1
|
||||
|
||||
''' <summary>
|
||||
''' 是否显示Ftp信息
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowFtpData() As Byte = 1
|
||||
|
||||
''' <summary>
|
||||
''' 显示信息的集合,对应数据位为1则打印,为0不打印
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowDataType() As Integer = ShowSystemData >> DebugPrintType.System Or
|
||||
ShowComportData >> DebugPrintType.Comport Or
|
||||
ShowDatabaseData >> DebugPrintType.Database Or
|
||||
ShowNetWorkData >> DebugPrintType.NetWork Or
|
||||
ShowFtpData >> DebugPrintType.Ftp
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 是否添加时间前缀
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowTime() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否显示与上包显示的间隔
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Property ShowTimeSpan() As Boolean = True
|
||||
|
||||
|
||||
Private Shared _lastShowTime As DateTime = Now
|
||||
|
||||
Private Shared _timeSpan As TimeSpan
|
||||
|
||||
''' <summary>
|
||||
''' 打印调试信息
|
||||
''' </summary>
|
||||
''' <param name="printString">需要打印的信息</param>
|
||||
Private Shared Sub DebugPrint(printString As String)
|
||||
Console.WriteLine(printString)
|
||||
End Sub
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 打印调试信息
|
||||
''' </summary>
|
||||
''' <param name="type">打印信息类型</param>
|
||||
''' <param name="printString">需要打印的信息</param>
|
||||
Public Shared Sub DebugPrint(type As DebugPrintType, printString As String)
|
||||
If ShowAllData = False Then Return
|
||||
If ((ShowDataType >> type) And 1) = 0 Then Return
|
||||
|
||||
_timeSpan = Now - _lastShowTime
|
||||
_lastShowTime = Now
|
||||
|
||||
Dim msgBuilder As New StringBuilder
|
||||
If ShowTime Then msgBuilder.Append($"{Now:yyyy-MM-dd HH:mm:ss} - ")
|
||||
If ShowTimeSpan Then msgBuilder.Append($"{_timeSpan.TotalMilliseconds,8} - ")
|
||||
msgBuilder.Append($"{type} - ")
|
||||
msgBuilder.Append($"{printString}")
|
||||
|
||||
DebugPrint(msgBuilder.ToString())
|
||||
End Sub
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 打印调试信息
|
||||
''' </summary>
|
||||
''' <param name="type">打印信息类型</param>
|
||||
''' <param name="tip">需要打印信息的提示前缀</param>
|
||||
''' <param name="printString">需要打印的信息</param>
|
||||
Public Shared Sub DebugPrint(type As DebugPrintType, tip As String, printString As String)
|
||||
DebugPrint(type, $"{tip}:{printString}")
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user