153 lines
5.6 KiB
VB.net
153 lines
5.6 KiB
VB.net
Imports System.Text
|
|
Imports System.IO
|
|
Imports Microsoft.VisualBasic.FileIO
|
|
|
|
Public Class Logger
|
|
Private Shared _instance As Logger
|
|
Private _logTextBox As TextBox
|
|
Private _maxLogEntries As Integer = 1000
|
|
|
|
Private Sub New()
|
|
' Private constructor for singleton pattern
|
|
End Sub
|
|
|
|
Public Shared ReadOnly Property Instance As Logger
|
|
Get
|
|
If _instance Is Nothing Then
|
|
_instance = New Logger()
|
|
End If
|
|
Return _instance
|
|
End Get
|
|
End Property
|
|
|
|
Public Sub SetLogTextBox(textBox As TextBox)
|
|
_logTextBox = textBox
|
|
End Sub
|
|
|
|
Public Sub LogRequest(method As String, url As String, headers As String, requestBody As String)
|
|
Dim logEntry As New StringBuilder()
|
|
logEntry.AppendLine("=== درخواست ارسالی ===")
|
|
logEntry.AppendLine($"زمان: {DateTime.Now:yyyy-MM-dd HH:mm:ss}")
|
|
logEntry.AppendLine($"متد: {method}")
|
|
logEntry.AppendLine($"آدرس: {url}")
|
|
|
|
If Not String.IsNullOrEmpty(headers) Then
|
|
logEntry.AppendLine($"هدرها: {headers}")
|
|
End If
|
|
|
|
If Not String.IsNullOrEmpty(requestBody) Then
|
|
logEntry.AppendLine($"بدنه درخواست: {requestBody}")
|
|
End If
|
|
|
|
logEntry.AppendLine("")
|
|
|
|
AddToLog(logEntry.ToString())
|
|
End Sub
|
|
|
|
Public Sub LogResponse(statusCode As String, responseBody As String, isSuccess As Boolean)
|
|
Dim logEntry As New StringBuilder()
|
|
logEntry.AppendLine("=== پاسخ دریافتی ===")
|
|
logEntry.AppendLine($"زمان: {DateTime.Now:yyyy-MM-dd HH:mm:ss}")
|
|
logEntry.AppendLine($"وضعیت: {If(isSuccess, "موفق", "ناموفق")}")
|
|
|
|
If Not String.IsNullOrEmpty(statusCode) Then
|
|
logEntry.AppendLine($"کد وضعیت: {statusCode}")
|
|
End If
|
|
|
|
If Not String.IsNullOrEmpty(responseBody) Then
|
|
' محدود کردن طول پاسخ برای نمایش بهتر
|
|
Dim displayResponse As String = responseBody
|
|
If displayResponse.Length > 500 Then
|
|
displayResponse = displayResponse.Substring(0, 500) & "..."
|
|
End If
|
|
logEntry.AppendLine($"پاسخ: {displayResponse}")
|
|
End If
|
|
|
|
logEntry.AppendLine("==========================================")
|
|
logEntry.AppendLine("")
|
|
|
|
AddToLog(logEntry.ToString())
|
|
End Sub
|
|
|
|
Public Sub LogError(errorMessage As String, exception As Exception)
|
|
Dim logEntry As New StringBuilder()
|
|
logEntry.AppendLine("=== خطا ===")
|
|
logEntry.AppendLine($"زمان: {DateTime.Now:yyyy-MM-dd HH:mm:ss}")
|
|
logEntry.AppendLine($"پیام خطا: {errorMessage}")
|
|
|
|
If exception IsNot Nothing Then
|
|
logEntry.AppendLine($"جزئیات خطا: {exception.Message}")
|
|
End If
|
|
|
|
logEntry.AppendLine("==========================================")
|
|
logEntry.AppendLine("")
|
|
|
|
AddToLog(logEntry.ToString())
|
|
End Sub
|
|
|
|
Public Sub LogInfo(message As String)
|
|
Dim logEntry As New StringBuilder()
|
|
logEntry.AppendLine($"زمان: {DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}")
|
|
logEntry.AppendLine("")
|
|
|
|
AddToLog(logEntry.ToString())
|
|
End Sub
|
|
|
|
Private Sub AddToLog(logText As String)
|
|
If _logTextBox IsNot Nothing Then
|
|
' استفاده از Invoke برای thread-safe بودن
|
|
If _logTextBox.InvokeRequired Then
|
|
_logTextBox.Invoke(Sub() AddLogText(logText))
|
|
Else
|
|
AddLogText(logText)
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub AddLogText(logText As String)
|
|
Try
|
|
' اضافه کردن متن جدید به ابتدای TextBox
|
|
_logTextBox.Text = logText & _logTextBox.Text
|
|
|
|
' محدود کردن تعداد ورودیهای لاگ
|
|
Dim lines As String() = _logTextBox.Text.Split(vbCrLf)
|
|
If lines.Length > _maxLogEntries Then
|
|
Dim newLines As String() = lines.Take(_maxLogEntries).ToArray()
|
|
_logTextBox.Text = String.Join(vbCrLf, newLines)
|
|
End If
|
|
|
|
' اسکرول به بالا برای نمایش آخرین لاگ
|
|
_logTextBox.SelectionStart = 0
|
|
_logTextBox.ScrollToCaret()
|
|
Catch ex As Exception
|
|
' در صورت خطا، لاگ را در فایل ذخیره کن
|
|
Try
|
|
Dim logPath As String = Path.Combine(SpecialDirectories.CurrentUserApplicationData, "hesabix_log.txt")
|
|
File.AppendAllText(logPath, logText)
|
|
Catch
|
|
' اگر حتی ذخیره فایل هم خطا داد، کاری نکن
|
|
End Try
|
|
End Try
|
|
End Sub
|
|
|
|
Public Sub ClearLog()
|
|
If _logTextBox IsNot Nothing Then
|
|
If _logTextBox.InvokeRequired Then
|
|
_logTextBox.Invoke(Sub() _logTextBox.Clear())
|
|
Else
|
|
_logTextBox.Clear()
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub SaveLogToFile()
|
|
Try
|
|
Dim logPath As String = Path.Combine(SpecialDirectories.CurrentUserApplicationData, $"hesabix_log_{DateTime.Now:yyyyMMdd_HHmmss}.txt")
|
|
File.WriteAllText(logPath, _logTextBox.Text)
|
|
LogInfo($"لاگ با موفقیت در فایل ذخیره شد: {logPath}")
|
|
Catch ex As Exception
|
|
LogError("خطا در ذخیره لاگ", ex)
|
|
End Try
|
|
End Sub
|
|
End Class
|