zoukankan      html  css  js  c++  java
  • 写入日志文件

    写入日志文件

    namespace HRPOWER.Service
    {
        public class WriteLog
        {
            private XmlDocument xmldoc = new XmlDocument();
            private string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "\" + "log";

            public WriteLog()
            {
                if (!Directory.Exists(logFilePath))
                {
                    Directory.CreateDirectory(logFilePath);
                }
            }

            public void write(string logTxt)
            {
                string fileName = logFilePath + "\" + DateTime.Now.ToString("yyyyMM") + "_log.txt";
                FileStream fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter streamWrite = new StreamWriter(fileStream);
                streamWrite.WriteLine(DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss :  ") + logTxt);

        //写入日志内容并换行
               streamWrite.Write(input+" ");
               streamWrite.Write("------------------------------------------ ");
                streamWrite.Flush();
                streamWrite.Close();
            }

                //判断文件是否存在及是否大于2K
                if(finfo.Length>1024 *1024 *10)
                {
                    //文件超过10MB则 重命名
                    File.Move(Directory.GetCurrentDirectory()+"\LogFile.txt",Directory.GetCurrentDirectory()+DateTime.Now.TimeOfDay+"\LogFile.txt");
                    finfo.Delete();
                  
                }
        }
    }

  • 相关阅读:
    day08作业
    day07作业
    day06作业
    day05作业
    OOAD与UML
    大数据(3):基于sogou.500w.utf8数据Hbase和Spark实践
    大数据(2):基于sogou.500w.utf8数据hive的实践
    大数据(1):基于sogou.500w.utf8数据的MapReduce程序设计
    九大排序算法的Java实现
    数字在排序数组中出现的次数
  • 原文地址:https://www.cnblogs.com/markli/p/3581936.html
Copyright © 2011-2022 走看看