C# 自定义日志类

转自:http://blog.csdn.net/l09302/article/details/18304299

 

 

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

namespace CommDataApp.Utils
{
    class Logger
    {

        //日志保存路径,不包括文件名
        private static string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "log";
        //日志完整路径,包括文件名
        //private static string file = filePath + "";
        private static string logFileName = filePath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";

        /// <summary>
        /// 创建文件夹和日志文件
        /// </summary>
        private static void CreateLogFile()
        {
            //文件不存在,则创建新文件
            if (!Directory.Exists(filePath))
            {
                try
                {
                    //按照路径创建目录
                    Directory.CreateDirectory(filePath);
                }
                catch (System.Exception e)
                {
                    throw new System.Exception(e + "创建目录失败!");
                }
            }
            if (!File.Exists(logFileName))
            {
                FileStream filestream = null;
                try
                {
                    filestream = File.Create(logFileName);
                    /*创建日志头部*/
                    filestream.Dispose();
                    filestream.Close();
                    CreateLogHead();
                }
                catch (System.Exception ex)
                {
                    throw new System.Exception(ex + "创建日志文件失败");
                }
            }
        }


        /// <summary>
        /// 创建日志头部
        /// </summary>
        private static void CreateLogHead()
        {
            System.IO.StreamWriter sw = null;
            try
            {
                sw = new System.IO.StreamWriter(logFileName, true, System.Text.Encoding.UTF8);
                sw.WriteLine();
                sw.WriteLine();
                sw.WriteLine("【日志创建时间:" + System.DateTime.Now.ToString() + "】【日志内容******************************************************】");
                sw.WriteLine();
                sw.WriteLine();
            }
            catch { }
            finally
            {
                sw.Flush();
                sw.Dispose();
                sw.Close();
            }
        }



        /// <summary>
        ///写入日志,
        ///自动创建文件夹和文件,
        ///日志文件创建到系统启动项根目录的Log文件夹下。
        /// </summary>
        /// <param name="logText">参数,日志内容</param>
        /// <returns>日志写入成功返回true,失败返回false</returns>
        public static void WriteLogToFile(string logText)
        {
            CreateLogFile();
            //true 如果日志文件存在则继续追加日志 
            System.IO.StreamWriter sw = null;
            try
            {
                sw = new System.IO.StreamWriter(logFileName, true, System.Text.Encoding.UTF8);
                sw.WriteLine("【  " + System.DateTime.Now.ToString() + "  】" + "【  " + logText + "  】");
                //return true;
            }
            catch (System.Exception ex)
            {
                //return false;
                throw new System.Exception(ex + "写入日志失败,检查!");
            }
            finally
            {
                sw.Flush();
                sw.Dispose();
                sw.Close();
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值