创建日志
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Out1.Log
{
class Log
{
public static void CreateLogText(string message)
{
string strPath;//文件的路径
DateTime dt = DateTime.Now;
try
{
strPath = Directory.GetCurrentDirectory() + "\\Log";//winform工程\\bin\目录下,创建日志文件夹
if (Directory.Exists(strPath)==false)//工程目录下Log目录是否存在,为true则没有此目录
{
Directory.CreateDirectory(strPath);//建立目录directory为目录对象
}
strPath = strPath + "\\" + dt.ToString("yyyy");
if (Directory.Exists(strPath)==false)
{
Directory.CreateDirectory(strPath);
}
strPath = strPath + "\\" + dt.Year.ToString() + "-" + dt.Month.ToString() + ".txt";
StreamWriter FileWriter = new StreamWriter(strPath, true);//创建日志文件
FileWriter.WriteLine("[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "]" +" "+ message);
FileWriter.Close();//关闭streamwrier对象
}
catch (Exception es)
{
string str = es.Message.ToString();
}
}
}
}