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