0

My program is generating a log, adding strings line by line. Final output (log file) is ending with .txt extension.

There are 3 types of lines: ERROR, WARNING and INFO.

I want to make the ERROR lines to be red, WARNING lines to be yellow, and INFO lines to be green. I'm working in C# and I am using Visual Studio Code.

How could I possibly do that kind of coloring?

NOTE: If I have to change the format of log file (not to be .txt anymore), I am okay with that.

Thanks a lot!

  • 3
    I don't think that you can change color inside .txt file, so that is not an option. – SᴇM Nov 01 '18 at 13:39
  • You can't change the color of text in a txt file. – Haytam Nov 01 '18 at 13:39
  • You mean you want a text editor to visualize your log files? Which logger are you using? Log4J, Log4Net, ...? – Rui Jarimba Nov 01 '18 at 13:39
  • Check this: https://stackoverflow.com/questions/14577568/is-there-a-user-friendly-log4net-log-files-viewer – Rui Jarimba Nov 01 '18 at 13:40
  • Try some other file type or keep 3 separate logs like ErrorLog WarningLog etc – sujith karivelil Nov 01 '18 at 13:40
  • 1
    You cannot put color to your text in *.txt. However, you can try a few options like create your own txt file viewer that detects these keywords and display with rich text box with colors. Otherwise, try this: https://stackoverflow.com/questions/12347587/how-to-create-rtf-from-plain-text-or-string-in-c – Casperonian Nov 01 '18 at 13:41
  • .txt won't work. You need something like .rtf. – Sweeper Nov 01 '18 at 13:42
  • Rich Text Format/HTML are the simplest file formats that support colours like that. Your 3 categories match those of the Windows Event Log so you could use that facility. – Alex K. Nov 01 '18 at 13:42
  • depends how you need it, ansi colour commands can be inserted into text files, however, most windows apps do not handle ansi colours by default. – BugFinder Nov 01 '18 at 13:44
  • In my program there are hard-coded strings for each message type, and when an error occurs - it just goes like: log.addLine(type: error, **message**: ) so in log.txt (output) it shows up like this: ERROR: [timestamp] Error occurred! Message: **** Well, I want this line to be red colored, if possible. If I have to change the type of log file, it is okay, I will. I am just struggling with how to paint the whole line. :( – Milan Radulović Nov 01 '18 at 13:47

1 Answers1

0

You can't put color in text file. If you have to, you could log into html and use the possibilities of html & css for coloring and formatting.
see Simple text to HTML conversion.

hce
  • 1,107
  • 9
  • 25