如何将信息文本写入带有颜色的日志文件?

时间:2012-09-01 19:42:19

标签: c#

今天我将日志记录写入文本文件日志:

/*----------------------------------------------------------------
 * 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 
         * -------------------------------*/

    }



}

而是将它写入一个简单的文本文件并在程序运行时使用Process和notepad.exe查看文件我认为可能有另一种方法来查看日志文件但内部有颜色?像richTextBox这样的东西,所以我可以在文件中绘制另一种颜色的每一行。

它会像记事本那样快速打开。

4 个答案:

答案 0 :(得分:3)

您不能拥有“带颜色”的文字文件。纯文本没有格式。

您需要编写具有格式的格式 - 例如RTF或HTML将起作用。

可能能够自定义您的日志和编程编辑器(例如记事本++),为您做一些语法高亮。

答案 1 :(得分:2)

记事本和纯文字不理解颜色。

如果它们对您很重要,则需要将日志写为某种格式化文件;例如:HTML,RTF等

答案 2 :(得分:0)

一个出色的日志文件查看器,具有可配置的颜色突出显示...   http://www.baremetalsoft.com/baretail/

答案 3 :(得分:0)

如果您有自己的应用程序内对话框/表单来查看日志文件,您还可以编写您解析并用于日志输入行着色的特定分隔文本信息。

我写了一个应用程序就是这样做的。

日志文件是纯ASCII文本,扩展名为.log文件。对于.log文件,我更喜欢纯ASCII文本,因为它是该文件类型的行业标准,因为它通常与Windows记事本相关联。

日志条目的格式设置为制表符分隔:日期/时间,条目类型(信息,警告,错误等)和条目文本消息。

当用户在应用程序中打开View Log表单时,我读取并解析制表符分隔的日志文件数据,并使用条目类型字段填充包含所有条目的多列ListView控件以确定ListView项目的图标图像及其文本颜色,例如。黑色表示信息,黄色表示警告,红色表示错误。