当前位置:网站首页>C#记录日志方法
C#记录日志方法
2022-07-07 07:47:00 【算盘】
public void WriteLogFile(string input)
{
string logPath = Directory.GetCurrentDirectory() + "\\LogFile";
//判断该路径下文件夹是否存在,不存在的情况下新建文件夹
if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}
//指定日志文件的目录
string fname = logPath + "\\LogFile.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(Directory.GetCurrentDirectory() + "\\LogFile.txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\LogFile.txt");
}
//创建只写文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter w = new StreamWriter(fs);
//设置写数据流的起始位置为文件流的末尾
w.BaseStream.Seek(0, SeekOrigin.End);
//写入“Log Entry : ”
w.Write("\n\rLog Entry : ");
//写入当前系统时间并换行
w.Write("{0} {1} \n\r", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
//写入日志内容并换行
w.Write(input + "\n\r");
//写入------------------------------------“并换行
w.Write("------------------------------------\n\r");
//清空缓冲区内容,并把缓冲区内容写入基础流
w.Flush();
//关闭写数据流
w.Close();
}
}
边栏推荐
- Applet popup half angle mask layer
- Delete a record in the table in pl/sql by mistake, and the recovery method
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- Win10安装VS2015
- Luogu p2482 [sdoi2010] zhuguosha
- 柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
- 2020ccpc Weihai J - Steins; Game (SG function, linear basis)
- ORM -- database addition, deletion, modification and query operation logic
- XML配置文件解析与建模
- C socke server, client, UDP
猜你喜欢
Win10安装VS2015
ISP、IAP、ICP、JTAG、SWD的编程特点
中国首款电音音频类“山野电音”数藏发售来了!
The landing practice of ByteDance kitex in SEMA e-commerce scene
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
ORM模型--数据记录的创建操作,查询操作
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
[untitled]
Postman interface test VI
ORM--查询类型,关联查询
随机推荐
Bean operation domain and life cycle
Writing file types generated by C language
内存==c语言1
嵌入式背景知识-芯片
Delete a record in the table in pl/sql by mistake, and the recovery method
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
MCU is the most popular science (ten thousand words summary, worth collecting)
ORM model -- creation and query of data records
Guys, have you ever encountered the case of losing data when Flink CDC reads mysqlbinlog? Every time the task restarts, there is a probability of losing data
Huffman encoded compressed file
Hcip first day notes sorting
Es classes and objects, prototypes
STM32产品介绍
Bean 作⽤域和⽣命周期
ORM--查询类型,关联查询
In addition to the objective reasons for overtime, what else is worth thinking about?
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
Win10 installation vs2015
CONDA creates virtual environment offline
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT