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 += "\r\n"; 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, , 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, , 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";
}
}
}
上一篇:leetcode 141. Linked List Cycle


下一篇:python第九十六天 ---Django(1)