Reputation: 11325
Today im writing the logging to a text file log:
/*----------------------------------------------------------------
* Module Name : Logger
* Description : A logger
* Author : Danny
* Date : 10/02/2010
* Revision : 1.00
* --------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
/*
* Introduction :
*
* This module is a logger. any module can use this
* module to log its actions.
*
*
* */
/*----------------------------------------
* P R I V A T E D E F I N I T I O N S
* ---------------------------------------*/
namespace DannyGeneral
{
class Logger
{
/*----------------------------------------
* P R I V A T E C O N S T A N T S
* ---------------------------------------*/
static string log_file_name = @"\logger.txt";
static string full_path_log_file_name;
static string path_log;
static Mutex mut;
/*----------------------------------------
* P R I V A T E V A R I A B L E S
* ---------------------------------------*/
/*---------------------------------
* P U B L I C M E T H O D S
* -------------------------------*/
/*----------------------------------------------------------
* Function : Logger
* Description : static Constructor
* Parameters : none
* Return : none
* --------------------------------------------------------*/
static Logger()
{
mut = new Mutex();
path_log = Path.GetDirectoryName(Application.LocalUserAppDataPath)+ @"\log";
if (!Directory.Exists(path_log))
{
Directory.CreateDirectory(path_log);
}
full_path_log_file_name = path_log + log_file_name;
}
/*----------------------------------------------------------
* Function : Write
* Description : writes a string to the log file
* This functions will add time and date and
* end of line chars to the string written to
* the file.
* Parameters : string to write to the file.
* Return : none
* --------------------------------------------------------*/
public static void Write(string str)
{
if (mut.WaitOne() == false)
{
return;
}
else
{
using (StreamWriter sw = new StreamWriter(full_path_log_file_name, true))
{
sw.Write(DateTime.Now.ToShortDateString() + "--" + DateTime.Now.ToShortTimeString() + " ==> " + str);
sw.WriteLine();
sw.Close();
}
}
mut.ReleaseMutex();
}
public static void exist()
{
if (!File.Exists(path_log + log_file_name))
{
StreamWriter sw = new StreamWriter(path_log + log_file_name);
sw.Write(DateTime.Now.ToShortDateString()+"--"+DateTime.Now.ToShortTimeString()+" ==> "+"First Time The Log File Was Created"+Environment.NewLine);
sw.WriteLine();
sw.Close();
}
}
public static void newEmptyLine()
{
StreamWriter sw = new StreamWriter(path_log + log_file_name,true);
sw.WriteLine();
sw.Close();
}
public static string LoggerPath()
{
string path = path_log + log_file_name;
return path;
}
/*---------------------------------
* P R I V A T E M E T H O D S
* -------------------------------*/
}
}
Instead to write it to a simple text file and viewing the file while the program is running using Process and notepad.exe i thought maybe there is another way to view the log file but with colors inside ? Something like richTextBox so i can paint in the file each line in another color.
And that it will be opened fast like it does now with notepad.
Upvotes: 0
Views: 6509
Reputation: 1623
If you have your own in-application dialog/form for viewing your log file, you can also write specific delimited text information that you parse and use for log entry line coloring.
I wrote one application that did exactly this.
The log file was plain ASCII text with a .log file extension. For .log files I prefer plain ASCII text as that is the industry standard for that file type, as it is typically associated with Windows Notepad.
Log entries were formatted as tab-delimited with: date/time, entry type (information, warning, error, etc.), and the entry text message.
When the user opens the View Log form in the application, I read and parse the tab-delimited log file data, and fill a multi-column ListView control with all of the entries, using the entry type field to determine the ListView Item's icon image and its text color, eg. black for information, yellow for warning, red for error.
Upvotes: 0
Reputation: 2228
A great log file viewer with configurable color highlighting... http://www.baremetalsoft.com/baretail/
Upvotes: 0
Reputation: 499072
You can't have a text file "with colors". Plain text does not have formatting.
You will need to write as a format that does have formatting - RTF or HTML for example will work.
You may be able to customize your logs and a programming editor (notepad++ for example) to do some syntax highlighting for you.
Upvotes: 4
Reputation: 51494
Notepad and pure text doesn't understand colours.
If they're important to you, you need to write the log as some kind of formatted file; eg: HTML, RTF, etc.
Upvotes: 2