zoukankan      html  css  js  c++  java
  • 20181015记录一个简单的TXT日志类

    20190422添加换行以及时间记录

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DAL
    {
        public class TXTLogHelper
        {
            /// <summary>
            /// 对某些操作进行TXT日志记录
            /// </summary>
            public static void LogBackup(string LogString)
            {
                //处理logstring,添加日期和换行
                LogString = DateTime.Now.ToString() + ":" +LogString;
                LogString += "
    ";
    
                string logFolder = GetOrCreateLogFilePath();
                string logFile = GetBackupLogFileName();
                
                FileInfo file = new FileInfo(logFile);
                FileStream fs = file.Open(FileMode.Append, FileAccess.Write);
                byte[] bytes = Encoding.UTF8.GetBytes(LogString);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close();
                fs.Dispose();
            }
    
            //获取目录路径,如果不存在则创建
            private static string GetOrCreateLogFilePath()
            {
                string backupFolder = System.Environment.CurrentDirectory + "\log";
                if (!Directory.Exists(backupFolder))
                    Directory.CreateDirectory(backupFolder);
                return backupFolder;
            }
    
            private static string GetBackupLogFileName()
            {
                //为了防止数据量过大,按照日期每天生成一个日志文件
                string logFileId = DateTime.Now.ToString("yyyy-MM-dd");
                return GetOrCreateLogFilePath() + "\" + logFileId + ".txt";
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Utilities
    {
        public class TXTLogHelper
        {
            /// <summary>
            /// 对某些操作进行TXT日志记录
            /// </summary>
            public static void LogBackup(string LogString)
            {
                string logFolder = GetOrCreateLogFilePath();
                string logFile = GetBackupLogFileName();
                
                FileInfo file = new FileInfo(logFile);
                FileStream fs = file.Open(FileMode.Append, FileAccess.Write);
                byte[] bytes = Encoding.UTF8.GetBytes(LogString);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close();
                fs.Dispose();
            }
    
            //获取备份目录路径,如果不存在则创建
            private static string GetOrCreateLogFilePath()
            {
                string backupFolder = System.Environment.CurrentDirectory + "\log";
                if (!Directory.Exists(backupFolder))
                    Directory.CreateDirectory(backupFolder);
                return backupFolder;
            }
    
            private static string GetBackupLogFileName()
            {
                //为了防止数据量过大,按照日期每天生成一个日志文件
                string logFileId = DateTime.Now.ToString("yyyy-MM-dd");
                return GetOrCreateLogFilePath() + "\" + logFileId + ".txt";
            }
        }
    }
  • 相关阅读:
    sed匹配多行并替换其中的内容
    sysbench 安装、使用和测试
    linux inode号已满的解决办法
    Linux双网卡绑定
    es安装
    kibana安装
    filebeat
    Codeforces 464E The Classic Problem (最短路 + 主席树 + hash)
    Codeforces 1137C Museums Tour (强连通分量, DP)
    HDU 4921 Map(状态压缩)
  • 原文地址:https://www.cnblogs.com/lovejunjuan/p/9789324.html
Copyright © 2011-2022 走看看