본문 바로가기

자료

[C#/vb.net] 오류 로그확인 및 오류 메시지 저장

728x90

 

블로그 인기글

● 메이플스토리 신규 룬패치/자동해제 프로그램 [링크 이동]

● 메이플스토리 거짓말탐지기 알림 프로그램 [링크 이동]

C#에서 TensorFlow 사용하는법 [링크 이동]


   Public el As New ErrorsAndEvents.ErrorLogger
        Try
            '// 여기에 프로그래밍
        Catch ex As Exception
            LOG(ex.StackTrace)    '// 프로그램에서 로그기록 함수
            el.WriteToErrorLog(ex.Message, ex.StackTrace, "GetAPI")    '// 오류 로그 기록
        End Try
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
 
<CLSCompliant(True)> _
Public Class ErrorLogger
 
    Public Sub New()
 
        'default constructor
 
    End Sub
 
 
 
    '*************************************************************
    'NAME:          WriteToErrorLog
    'PURPOSE:       Open or create an error log and submit error message
    'PARAMETERS:    msg - message to be written to error file
    '               stkTrace - stack trace from error message
    '               title - title of the error file entry
    'RETURNS:       Nothing
    '*************************************************************
    Public Sub WriteToErrorLog(ByVal msg As String, ByVal stkTrace As String, ByVal title As String)
 
        'check and make the directory if necessary; this is set to look in the application
        'folder, you may wish to place the error log in another location depending upon the
        'the user's role and write access to different areas of the file system
        If Not System.IO.Directory.Exists(Application.StartupPath & "\Errors\") Then
            System.IO.Directory.CreateDirectory(Application.StartupPath & "\Errors\")
        End If
 
        'check the file
        Try
            Using fs As FileStream = New FileStream(Application.StartupPath & "\Errors\errlog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
                Dim s As StreamWriter = New StreamWriter(fs)
                s.Close()
                fs.Close()
            End Using
        Catch ex As Exception
 
        End Try
 
        'log it
        Try
            Using fs1 As FileStream = New FileStream(Application.StartupPath & "\Errors\errlog.txt", FileMode.Append, FileAccess.Write)
                Dim s1 As StreamWriter = New StreamWriter(fs1)
                s1.Write("Title: " & title & vbCrLf)
                s1.Write("Message: " & msg & vbCrLf)
                s1.Write("StackTrace: " & stkTrace & vbCrLf)
                s1.Write("Date/Time: " & DateTime.Now.ToString() & vbCrLf)
                s1.Write("===========================================================================================" & vbCrLf)
                s1.Close()
                fs1.Close()
            End Using
        Catch ex As Exception
 
        End Try
 
    End Sub
 
End Class
728x90