Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Call WriteLog("Button1_Clickを実行します。")
Call WriteLog("これはテストです。")
Catch ex As Exception
MsgBox("例外が発生しました。" & vbNewLine & ex.Message)
End Try
End Sub
Private Sub WriteLog(ByVal Value As String)
'▼文字数チェック
If Len(Value) > 10 Then
Throw New ApplicationException("ログに文字以上は書き込めません。")
End If
'▼全角文字が混ざっていないかチェック
If Len(Value) <> System.Text.Encoding.GetEncoding("Shift-JIS").GetByteCount(Value) Then
Throw New ApplicationException("ログには全角文字は書き込めません。")
End If
'▼書き込み実行
Dim Writer As New IO.StreamWriter("C:\Log\MyAppLog.txt", True)
Dim strTime As String
strTime = Now.ToString("yyyy/MM/dd HH:mm:ss ")
Writer.WriteLine(strTime & Value)
Writer.Close()
End Subネタ元