Help formatting log output

Welcome Forums General PowerShell Q&A Help formatting log output

Viewing 4 reply threads
  • Author
    Posts
    • #278499
      Participant
      Topics: 10
      Replies: 203
      Points: 996
      Helping Hand
      Rank: Major Contributor

      I am trying to find the best method to format my PowerShell logging. What I am trying to achieve is the following. I want to precede all lines sent to the log file with the following:

      [07-30-2020 17:15:44 -0700 INFO] yada yada yada ….

      So, precede log entries with [month-day-year hours:minutes:seconds timezone INFO] similar to Splunk logging.

      I have no problems getting this format, but it seems like a performance hit to calculate the date/time information for each line logged (of which there are many). Is there a better way then running Get-Date for each line logged?

      I should also point out this is all run through Start-Transcript.

      Thanks in advance for any help/suggestions.

    • #278505
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,988
      Helping Hand
      Rank: Community Hero

      Have you tried .NET methods? I would imagine that Get-Date is just a wrapper for these. Would have to use ToString() or string format after you have the date.

    • #278514
      Participant
      Topics: 10
      Replies: 203
      Points: 996
      Helping Hand
      Rank: Major Contributor

      Thanks a million Rob. You have been as usual very helpful 🙂

      https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=net-5.0

       

    • #278520
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,988
      Helping Hand
      Rank: Community Hero

      Oops, totally meant to put a link in there:

      #PSTip Working with DateTime objects in PowerShell using [System.DateTime]

      Also, just a quick test shows that Get-Date is actually faster…

    • #278529
      Participant
      Topics: 10
      Replies: 203
      Points: 996
      Helping Hand
      Rank: Major Contributor

      Thanks again Rob. I am going with

      [DateTimeOffset]::Now

      as it gets me both the Date/Time and the TimeZone in one command and I am including both in my log prefix.

      My testing was a tad different then yours, just an FYI

      PS C:\> Measure-Command {[DateTimeOffset]::Now}

      Days : 0
      Hours : 0
      Minutes : 0
      Seconds : 0
      Milliseconds : 0
      Ticks : 1087
      TotalDays : 1.25810185185185E-09
      TotalHours : 3.01944444444444E-08
      TotalMinutes : 1.81166666666667E-06
      TotalSeconds : 0.0001087
      TotalMilliseconds : 0.1087

       

      PS C:\> Measure-Command {get-date}

      Days : 0
      Hours : 0
      Minutes : 0
      Seconds : 0
      Milliseconds : 0
      Ticks : 2985
      TotalDays : 3.45486111111111E-09
      TotalHours : 8.29166666666667E-08
      TotalMinutes : 4.975E-06
      TotalSeconds : 0.0002985
      TotalMilliseconds : 0.2985

Viewing 4 reply threads
  • You must be logged in to reply to this topic.