Log日志类

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

namespace Itcase.log
{
    public class Log
    {
        public static void WriteLog(string Module, string msg)
        {
            string Err_str = "";
            string Err_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");

            Err_str = Err_Time + " -> [" + Module + "] -> " + msg;

            string strDir = Directory.GetCurrentDirectory() + @"LogErrorLog_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            try
            {
                FileStream fs = null;
                if (File.Exists(strDir))
                {
                    fs = new FileStream(strDir, FileMode.Append, FileAccess.Write);
                }
                else
                {
                    Directory.CreateDirectory(Directory.GetCurrentDirectory() + @"Log");
                    fs = new FileStream(strDir, FileMode.Create);
                }
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(Err_str);
                sw.Flush();
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void WriteLogs(string msg)
        {
            string Err_str = "";
            string Err_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");

            Err_str = Err_Time + " -> Reason :" + msg;

            string strDir = Directory.GetCurrentDirectory() + @"LogErrorLog_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            try
            {
                FileStream fs = null;
                if (File.Exists(strDir))
                {
                    fs = new FileStream(strDir, FileMode.Append, FileAccess.Write);
                }
                else
                {
                    Directory.CreateDirectory(Directory.GetCurrentDirectory() + @"Log");
                    fs = new FileStream(strDir, FileMode.Create);
                }
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(Err_str);
                sw.Flush();
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/YuanDong1314/p/13031913.html