Logger Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

Logger


Objetive:

Create a class Logger, with a static method Write, which will append a certain text to a file: Logger.Write("myLog.txt", "This text is being logged");

It must also include the current date and time before the text (in the same line), so that the log file is easier to analyze.

Hint: find information about "AppendText" and about "DateTime.now"


Code:

Imports System
Imports System.IO
Namespace LoggerAplication
    Class Logger
        Public Shared Sub Write(ByVal nameFile As String, ByVal text As String)
            Dim myFile As StreamWriter
            myFile = File.AppendText(nameFile)
            myFile.WriteLine(DateTime.Now & " - " & text)
            myFile.Close()
        End Sub
    End Class
End Namespace

Namespace LoggerAplication
    Class Program
        Private Shared Sub Main()
            Logger.Write("text.txt", "Hola")
        End Sub
    End Class
End Namespace

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.