日志文件类

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace CreditImport
{
    class LogManager
    {
        private static string logPath = @"log\";               // 日志文件夹
        private static string logFolder = "log";               // 文件夹名
        private static string lonExtension = ".Log";           // 日志文件类型

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(LogFile logFile, string msg)
        {
            WriteLog(logFile.ToString(), msg);
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFolder,LogFile logFile, string msg)
        {
            WriteLog(logFolder,logFile.ToString(), msg);
        }
       
        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFile, string msg)
        {
            try
            {
                DirectoryExists();
                DeleteLog(logFile, "");
                System.IO.StreamWriter sw = System.IO.File.AppendText(AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + lonExtension);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    " + msg);
                sw.Close();
            }
            catch
            { }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFolder,string logFile, string msg)
        {
            try
            {
                DirectoryExists(logFolder);
                DeleteLog(logFile, "");
                System.IO.StreamWriter sw = System.IO.File.AppendText(AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + lonExtension);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    " + msg);
                sw.Close();
            }
            catch
            { }
        }


        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="logFile">日志文件类型</param>
        /// <param name="logFileName">日志文件名</param>
        /// <param name="msg">信息说明</param>
        public static void WriteLog(string logFile, string logFileName, ArrayList al)
        {
            try
            {
                DirectoryExists();
                DeleteLog(logFile,logFileName);
                System.IO.StreamWriter sw = System.IO.File.AppendText(AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + "_" + logFileName + lonExtension);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                for (int i = 0; i < al.Count; i++)
                {
                    sw.WriteLine(al[i].ToString());
                }
                sw.Close();
            }
            catch
            { }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="logFolder">日志文件夹</param>
        /// <param name="logFile">日志文件类型</param>
        /// <param name="logFileName">日志文件名</param>
        /// <param name="msg">信息说明</param>
        public static void WriteLog(string logFolder,string logFile, string logFileName, ArrayList al)
        {
            try
            {
                DirectoryExists(logFolder);
                DeleteLog(logFile, logFileName);
                System.IO.StreamWriter sw = System.IO.File.AppendText(AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + "_" + logFileName + lonExtension);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                for (int i = 0; i < al.Count; i++)
                {
                    sw.WriteLine(al[i].ToString());
                }
                sw.Close();
            }
            catch
            { }
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        public static void DirectoryExists()
        {
            if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + logFolder) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + logFolder);
            }
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="logFileName"></param>
        public static void DirectoryExists(string logFolder)
        {
            if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + logFolder) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + logFolder);
            }
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="logFile"></param>
        /// <param name="logFileName"></param>
        public static void DeleteLog(string logFile, string logFileName)
        {
            string logPathName = AppDomain.CurrentDomain.BaseDirectory + logPath + logFile + "_" + logFileName + lonExtension;
            if (File.Exists(logPathName))
            {
                File.Delete(logPathName);
            }
        }
       
    }

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogFile
    {
        Trace,
        Warning,
        Error,
        SQL
    }
}

posted @ 2010-12-23 10:12  小さいです哥  阅读(503)  评论(4编辑  收藏  举报