Skip to sidebar Skip to content
Ipercube Technical
Jack of all trades, master of none
Date: Time: - Stardate:
Welcome, Log in by clicking  Here!
SS64
MDN
Stack
Code
GitHub
Wiki
Convert
V Studio
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions
Ipercube Technical
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions

Open Log

Initializes and writes the header for a log file, optionally preserving any existing content.

This function generates a log file with a timestamped header containing application metadata such as product name, version, platform, framework, and copyright. If the file already exists and PreserveLogFile is False , it will be deleted. The full path is constructed if not explicitly provided, and the file name is remembered for use by subsequent log operations.

VB
    Public Sub OpenLog(Optional LogFileName As String = "", 
                       Optional PreserveLogFile As Boolean = False, 
                       Optional callingAssembly As Assembly = Nothing)

        Dim asm As Assembly

        If callingAssembly Is Nothing Then
            asm = Assembly.GetEntryAssembly()
            If asm Is Nothing Then asm = Assembly.GetExecutingAssembly
        Else
            asm = callingAssembly
        End If

        Static DefaultLogFileName As String = String.Empty

        'Set Default file name
        If DefaultLogFileName = String.Empty Then
            If Not asm Is Nothing Then
                DefaultLogFileName = CType(asm.GetCustomAttribute(GetType(AssemblyProductAttribute)), 
                                     AssemblyProductAttribute).Product + ".log"
            Else
                DefaultLogFileName = $"Log file created by {My.Application.Info.ProductName}.log"
            End If
            DefaultLogFileName = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%"), 
                                 Environment.ExpandEnvironmentVariables("%HOMEPATH%"), LogFileName)
        End If

        If String.IsNullOrEmpty(LogFileName) Then LogFileName = DefaultLogFileName

        Dim szRowHeader As String = CStr(Now) & "   "
        Dim szMessage As String = String.Empty

        'Delete old Log file
        If Not PreserveLogFile Then
            Try : Kill(LogFileName) : Catch ex As Exception : End Try
        End If

        'Write Header
        If Not File.Exists(LogFileName) Then

            File.AppendAllText(LogFileName, szRowHeader & LineSep & Environment.NewLine)
            Console.WriteLine(szRowHeader & LineSep)

            If Not asm Is Nothing Then

                Dim assemblyInfo = GetAssemblyInfo(asm)

                szMessage = $"{assemblyInfo.Product}  -  {assemblyInfo.Version}  -  " + 
                            $"{assemblyInfo.Platform}  {assemblyInfo.Framework}"
                File.AppendAllText(LogFileName, szRowHeader & szMessage & Environment.NewLine)
                Console.WriteLine(szRowHeader & szMessage)

                File.AppendAllText(LogFileName, szRowHeader & LineSep & Environment.NewLine)
                Console.WriteLine(szRowHeader & LineSep)

                Dim szCurrentYear As String = CStr(DateTime.Now.Year)
                Dim copyright As String = assemblyInfo.Copyright

                copyright = If(copyright.Contains("{0}"), String.Format(copyright, szCurrentYear), copyright)

                Dim matches = Regex.Matches(copyright, Regex.Escape(szCurrentYear), RegexOptions.IgnoreCase)
                If matches.Count > 1 Then
                    copyright = copyright.Replace($"{szCurrentYear}-{szCurrentYear}", szCurrentYear)
                End If

                File.AppendAllText(LogFileName, szRowHeader & copyright & Environment.NewLine)
                Console.WriteLine(szRowHeader & assemblyInfo.Copyright)
            Else
                szMessage = "Unable to get information from EntryAssembly"
                File.AppendAllText(LogFileName, szRowHeader & szMessage & Environment.NewLine)
                Console.WriteLine(szRowHeader & szMessage)
            End If

            File.AppendAllText(LogFileName, szRowHeader & LineSep & Environment.NewLine)
            Console.WriteLine(szRowHeader & LineSep)
        End If

        'Save the Log file name for suseqeunt call to WriteLog
        WriteLog(JustRememberTheLogFileName, LogFileName)

    End Sub

Posted by ipercube


© 2026 Ipercube Design