一个极其简易文本日志类

namespace MyLogger
{
    public class NetLog
    {
        private static object lockObj = new object();
        /// <summary>
        /// 写入日志到文本文件
        /// </summary>
        /// <param name="strMessage">日志内容</param>
        public static void WriteTextLog(string strMessage)
        {
            lock(lockObj)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                DateTime time = DateTime.Now;
                string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
                StringBuilder str = new StringBuilder();

                str.Append("Time:" + time + ";Message: " + strMessage + "\r\n");
                StreamWriter sw;
                if (!File.Exists(fileFullPath))
                {
                    sw = File.CreateText(fileFullPath);
                }
                else
                {
                    sw = File.AppendText(fileFullPath);
                }
                sw.WriteLine(str.ToString());
                sw.Close();
            }

        }
    }
}

 

上一篇:Luogu P4555 [国家集训队]最长双回文串


下一篇:Manacher算法