日志工具类-RiZhiHelp

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

namespace RiZhi
{
    public class RiZhiHelp
    {
        private const string Path = @"E:\自己学习\RiZhi\Log\";

        public static void WriteLog(string msg)
        {
            DateTime Now = DateTime.Now;
            string Nian = Now.Year.ToString();
            string Yue = Now.Month.ToString();
            if (!Directory.Exists(string.Format("{0}{1}", Path, Now)))
            {
                Directory.CreateDirectory(string.Format("{0}{1}", Path, Nian));
            }
            if (!Directory.Exists(string.Format(@"{0}{1}\{2}", Path, Now, Yue)))
            {
                Directory.CreateDirectory(string.Format(@"{0}{1}\{2}", Path, Nian, Yue));
            }
            FileStream fs = new FileStream(string.Format(@"{0}{1}\{2}\{3}_LOG.txt", Path, Nian, Yue, Now.ToString("yyyy-MM-dd")), FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + msg + "\n");
            m_streamWriter.Flush();
            m_streamWriter.Close();
            fs.Close();
        }
    }
}

调用:

RiZhiHelp.WriteLog("123");

你可能感兴趣的:(asp.net常用功能和代码)