/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:日志相关的工具类
*│ 作 者:执笔小白
*│ 版 本:1.0
*│ 创建时间:2021-10-13 15:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: WMSTOMESTT
*│ 类 名:ETools
*└──────────────────────────────────────────────────────────────┘
*/
using System;
using System.IO;
namespace WMSTOMESTT
{
public class ETools
{
// 写日志
public static void WriteLogFile(string input, string txtName)
{
try
{ // exe的目录,web的请用:System.Web.Hosting.HostingEnvironment
// string logAdress = System.Environment.CurrentDirectory.ToString() + "\\Log\\"; // exe.config
string logAdress = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString() + "\\Log\\"; // dll.config
if (!System.IO.Directory.Exists(logAdress))
{
System.IO.Directory.CreateDirectory(logAdress);//不存在就创建目录
}
string adress = logAdress + txtName;
if (!System.IO.Directory.Exists(adress))
{
System.IO.Directory.CreateDirectory(adress);//不存在就创建目录
}
// string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString();
/**/
///指定日志文件的目录
string fname = adress + "\\" + "log" + DateTime.Now.ToString("yy-MM-dd") + ".txt";
/**/
///定义文件信息对象
FileInfo finfo = new FileInfo(fname);
if (!finfo.Exists)
{
FileStream fs;
fs = File.Create(fname);
fs.Close();
finfo = new FileInfo(fname);
}
/**/
///判断文件是否存在以及是否大于2K
if (finfo.Length > 1024 * 1024 * 10)
{
/**/
///文件超过10MB则重命名
File.Move(logAdress + "\\Log\\" + txtName + ".txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\Log\\" + txtName + ".txt");
/**/
///删除该文件
//finfo.Delete();
}
//finfo.AppendText();
/**/
///创建只写文件流
using (FileStream fs = finfo.OpenWrite())
{
/**/
///根据上面创建的文件流创建写数据流
StreamWriter w = new StreamWriter(fs);
/**/
///设置写数据流的起始位置为文件流的末尾
w.BaseStream.Seek(0, SeekOrigin.End);
w.WriteLine("-----------------------Start-----------------------");
w.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
/**/
///写入当前系统时间并换行
/**/
///写入日志内容并换行
w.WriteLine(input);
/**/
///写入------------------------------------“并换行
w.WriteLine("------------------------END------------------------");
/**/
///清空缓冲区内容,并把缓冲区内容写入基础流
w.Flush();
/**/
///关闭写数据流
w.Close();
}
}
catch (Exception ex)
{ throw ex; }
}
}
#region 例子
public class ETest
{
public void WLogDemo()
{
StringBuilder strHead = new StringBuilder();
strHead.AppendLine("*****记录*****");
ETools.WriteLogFile(strHead.ToString(), "记录文件名");
}
}
#endregion
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
补充:1.log4net:log4net学习笔记(一)-WPF(简单使用-目前目录格式较单一)
2.对本文单线程日志helper的简单扩展:C#多线程日志Helper
3.中文乱码请使用UTF-8:new StreamWriter(fs, Encoding.UTF8);
作者:꧁执笔小白꧂