using System;
using System.IO;
using System.Text;
namespace Core
{
public class LogHelper
{
private static void writeLog(string log)
{
string strFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs\\log.txt";
string strDirPath = Path.GetDirectoryName(strFilePath);
if (!Directory.Exists(strDirPath))//Directory.Exists(dirPath)目录的路径
Directory.CreateDirectory(strDirPath);//Directory.CreateDirectory(path)创建目录
strFilePath = Path.Combine(strDirPath, "log" + string.Format("{0:yyyyMMdd}", DateTime.Now) + ".txt");//Path.Combine(strpath1,strpath2)
if (!File.Exists(strFilePath))//File.Exists(path),指定路径的文件是否存在
{
//FileStream fsCreate = File.Create(strFilePath);//创建路径
FileStream fsCreate = new FileStream(strFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fsCreate.Close();
}
FileStream fs = new FileStream(strFilePath, FileMode.Append,FileAccess.Write);//任何读取操作都会报异常
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.WriteLine("--------------------------------------------");
sw.WriteLine("Timestamp: "+DateTime.Now.ToString());
sw.WriteLine("Message: " + log);
sw.WriteLine("--------------------------------------------");
sw.Close();
fs.Close();
}
//打印普通信息
public static void WriteInfo(string objectName, string info)
{
writeLog(objectName + ":" + info);
}
public static void WriteInfo(string objectName, string info, int lines)
{
string strlines = "";
for (int i = ; i < lines; i++)
{
strlines += "\r\n";
}
writeLog(objectName+":"+info);
//writeLog(strlines + DateTime.Now.ToString() + " INFO " + objectName + " " + info);
}
//打印错误信息
public static void WriteError(string objectName, string error)
{
writeLog(objectName+":"+error);
//writeLog(DateTime.Now.ToString() + " ERROR " + objectName + " " + error);
}
public static void WriteError(string objectName, string error, int lines)
{
string strlines = "";
for (int i = ; i < lines; i++)
{
strlines += "\r\n";
}
writeLog(objectName + ":" + error);
//writeLog(strlines + DateTime.Now.ToString() + " ERROR " + objectName + " " + error);
}
}
}