Public Class DEBUG
Public path As String
Public handle As StreamWriter
Public handleRead As StreamReader
' ********************************************************
' (コンストラクタの定義)( Sub で定義する )
' ********************************************************
Public Sub New(ByVal Messaege As String)
#If DEBUG Then
path = Application.StartupPath()
path &= "\debug.log"
handle = New StreamWriter(path, True)
Me.Write(Application.ExecutablePath & " を開始しました")
Me.Write("インスタンス作成時:" & Messaege)
Dim pc As String = System.Net.Dns.GetHostName()
Dim ip As String = System.Net.Dns.GetHostEntry(pc).AddressList(0).ToString()
Dim login As String = Environment.UserName
Me.Write("****** " & login & vbTab & pc & vbTab & ip & " ******")
#End If
End Sub
' ********************************************************
' 書き込み
' ********************************************************
Public Sub Write(Optional ByVal Messaege As String = "")
#If DEBUG Then
Dim str As String = DateTime.Now.ToString()
str &= vbTab & Messaege
handle.WriteLine(str)
#End If
End Sub
' ********************************************************
' 終了
' ********************************************************
Public Sub Quit()
#If DEBUG Then
Me.Write(Application.ExecutablePath & " を終了します")
Me.Write()
handle.Flush()
handle.Close()
#End If
End Sub
' ********************************************************
' 読み込み用に変更
' ********************************************************
Public Sub ChangeRead()
#If DEBUG Then
Me.Write("ログを読み込み用に変更します")
handle.Flush()
handle.Close()
handleRead = New StreamReader(path, Encoding.UTF8)
#End If
End Sub
' ********************************************************
' 読み込みを終了して書き込みモードに戻る
' ********************************************************
Public Sub EndRead()
#If DEBUG Then
handleRead.Close()
handleRead.Dispose()
handle = New StreamWriter(path, True)
Me.Write("書き込みを再開します")
#End If
End Sub
End Class
関連する記事
アプリケーションイベントとしてのテバッグオブシェクトの実装
Namespace My
Partial Friend Class MyApplication
Public Debug As DEBUG
' ********************************************************
' スタートアップ
' ********************************************************
Private Sub MyApplication_Startup(ByVal sender As Object, _
ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
Dim str As String
Dim line As String = ""
For Each str In e.CommandLine
line &= str & " "
Next
Debug = New DEBUG("コマンドライン:" & line)
Debug.Write("スタートアップを開始しました")
End Sub
' ********************************************************
' シャットダウン
' ********************************************************
Private Sub MyApplication_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Shutdown
Debug.Write("シャットダウンイベントを実行しています")
Debug.Quit()
End Sub
End Class
End Namespace
Form クラスからの利用
Partial Class Form1
' ******************************************************
' 初期処理
' ( フォームは表示されていない )
' ******************************************************
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' デバッグモードログ
My.Application.Debug.Write("初期処理を開始しています")
End Sub
End Class
ログ読み込みのサンプル
ログデータ.AddColumn("Date", "日付")
ログデータ.AddColumn("Time", "時間")
ログデータ.AddColumn("Message", "メッセージ")
My.Application.Debug.ChangeRead()
Dim str As String
Dim data() As String
Dim dataDate() As String
Dim delimiter As Char() = {}
Dim i As Integer
Do While Not My.Application.Debug.handleRead.EndOfStream
str = My.Application.Debug.handleRead.ReadLine()
ログデータ.AddRow()
delimiter = vbTab.ToCharArray()
data = str.Split(delimiter)
delimiter = " ".ToCharArray()
dataDate = data(0).Split(delimiter)
ログデータ.SetColumnText("Date", dataDate(0))
ログデータ.SetColumnText("Time", dataDate(1))
str = ""
For i = 1 To data.GetUpperBound(0)
If str <> "" Then
str += " "
End If
str += data(i)
Next
ログデータ.SetColumnText("Message", str)
If str = "スタートアップを開始しました" Then
ログデータ.Rows(ログデータ.nCurrentRow).Cells("Message").Style.BackColor = Color.Aqua
End If
Loop
My.Application.Debug.EndRead()