Imports System.Data.SqlClient
Imports System.IO
Imports System.Text

Namespace AdminLog

    ''' <summary>
    ''' 应用程序日志
    ''' </summary>
    Public NotInheritable Class ApplicationLog

        ''' <summary>
        ''' 日志类型
        ''' </summary>
        Public Enum LogType
            ''' <summary> 堆栈跟踪信息 </summary>
            Trace
            ''' <summary> 警告信息 </summary>
            Warning
            ''' <summary> 错误信息应该包含对象名、发生错误点所在的方法名称、具体错误信息 </summary>
            [Error]
            ''' <summary> 与数据库相关的信息 </summary>
            Database
        End Enum


        ''' <summary>
        ''' 日志文件所在父文件夹路径
        ''' </summary>
        Private Shared _logPath As String = My.Application.Info.DirectoryPath

        ''' <summary>
        ''' 日志文件名前缀
        ''' </summary>
        Private Shared _logFilePrefix As String = "Log"

        ''' <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 = My.Application.Info.DirectoryPath
                End If
                Return _logPath
            End Get
            Set(ByVal 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


        ''' <summary>
        ''' 日志文件路径
        ''' </summary>
        ''' <returns></returns>
        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($"ErrorMessage:{ex.Message}{vbNewLine}")
            msg.Append($"ErrorTime:{Now}{vbNewLine}")
            msg.Append($"ErrorSource:{ex.Source}{vbNewLine}")
            msg.Append($"ErrorType:{ex.GetType}{vbNewLine}")
            msg.Append($"ErrorTargetSite:{ex.TargetSite}{vbNewLine}")
            msg.Append($"ErrorStackTrace:{ex.StackTrace}{vbNewLine}")
            WriteLog(LogType.Error, msg.ToString())
        End Sub

        ''' <summary>
        ''' 写入错误信息记录日志
        ''' </summary>
        ''' <param name="msg"></param>
        Public Shared Sub WriteErrorLog(msg As String)
            WriteLog(LogType.Error, msg.ToString())
        End Sub

        ''' <summary>
        ''' 写入流程信息记录日志
        ''' </summary>
        ''' <param name="msg"></param>
        Public Shared Sub WriteTraceLog(msg As String)
            WriteLog(LogType.Trace, msg.ToString())
        End Sub


        ''' <summary>
        ''' 写入警告信息记录日志
        ''' </summary>
        ''' <param name="msg"></param>
        Public Shared Sub WriteWarningLog(msg As String)
            WriteLog(LogType.Warning, msg.ToString())
        End Sub


        ''' <summary>
        ''' 写入数据库信息记录日志
        ''' </summary>
        ''' <param name="msg"></param>
        Public Shared Sub WriteDatabaseLog(msg As String)
            WriteLog(LogType.Database, msg.ToString())
        End Sub


        ''' <summary>
        ''' 日志锁，防止多线程同时写日志导致冲突
        ''' </summary>
        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.ToString("yyyy-MM-dd HH:mm:ss:fff ")}]")
                    logString.Append($"[{logType.PadRight(8)}]")
                    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


        ''' <summary>
        ''' 写入日志到本地
        ''' </summary>
        Public Shared Sub WriteLog(type As LogType, ByVal msg As String)
            WriteLog(type.ToString(), msg)
        End Sub



    End Class
End Namespace