日志类(常用的都是用log4net,这里简陋地实现一个写入文本日志类)

时间:2021-05-27 23:29:33

    /// <summary>
    /// 日志类(常用的都是用log4net,这里简陋地实现一个写入文本日志类)
    /// </summary>
    public static class Logger
    {
        /// <summary>
        /// 写入异常日志
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteFileLog(string exMsg, string path)
        {
            FileStream fs = null;
            StreamWriter m_streamWriter = null;
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                path = path + "//" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine(DateTime.Now.ToString() + "/n");
                m_streamWriter.WriteLine("-----------------------------------------------------------");
                m_streamWriter.WriteLine("-----------------------------------------------------------");
                m_streamWriter.WriteLine(exMsg);
                m_streamWriter.WriteLine("-----------------------------------------------------------");
                m_streamWriter.WriteLine("-----------------------------------------------------------");
                m_streamWriter.Flush();
            }
            finally
            {
                if (m_streamWriter != null)
                {
                    m_streamWriter.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }