您的位置:首页 > 编程语言 > C#

C# 实现 保存log到文件的类

2011-08-30 11:17 211 查看
 
using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace PAMS2CE.PUBLIC

{

    class Log

    {

        private enum ErrorType

        {

            W = 0,      //警告信息

            E = 1,      //错误信息

            I = 2,      //普通信息

        }

        private  static string LogOutputFile = "\\Storage Card\\log.txt";

        private  static ErrorType errType = Log.ErrorType.I;

        private static bool WriteLogToFile(string module,string strlog)

        {

            try

            {

                if (File.Exists(Log.LogOutputFile) == false)

                {

                   FileStream fsm = File.Create(Log.LogOutputFile);

                   if (fsm != null)

                       fsm.Close();

                }

                DateTime dt = File.GetCreationTime(Log.LogOutputFile);

                dt = dt.AddDays(3);

                //删除三天前log信息

                if (dt.CompareTo(DateTime.Now) < 0)

                {

                    File.Delete(Log.LogOutputFile);

                    //File.Create(Log.LogOutputFile);

                }

                //时间

                string inputstr = DateTime.Now.ToString();

                inputstr += "  ";

                //错误类型

                inputstr += errType.ToString();

                inputstr += "  ";

                //模块名,只保留20个字符,文本统一

                string modulename = "";

                if (module.Length >= 20)

                    modulename = module.Substring(0, 20);

                else

                {

                    modulename = module;

                    do

                    {

                        modulename = modulename.Insert(modulename.Length, "-");

                    }

                    while (modulename.Length < 20);

                }

                //modulename = module;

                inputstr += modulename;

                inputstr += "  ";

                //log信息

                inputstr += strlog;

                //FileStream ot = File.OpenWrite(LogOutputFile);

               

                //ot.w

                StreamWriter output = File.AppendText(Log.LogOutputFile);

                output.WriteLine(inputstr);

                //output.

                output.Close();

            }

            catch (Exception)

            {

                return false;

            }

            return true;

        }

        public static bool I(string module,string strlog)

        {

            Log.errType = ErrorType.I;

            return WriteLogToFile(module, strlog);

        }

        public static bool W(string module,string strlog)

        {

            Log.errType = ErrorType.W;

            return WriteLogToFile(module, strlog);

        }

        public static bool E(string module,string strlog)

        {

            Log.errType = ErrorType.E;

            return WriteLogToFile(module, strlog);       

        }

        public static void Clear()

        {

            if (File.Exists(Log.LogOutputFile) == true)

                File.Delete(Log.LogOutputFile);

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息