码迷,mamicode.com
首页 > 其他好文 > 详细

文件 日志 写入 与读取

时间:2018-03-27 16:36:15      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:line   datetime   odi   lin   cat   ext   head   ret   obj   

 

private static string logPath = @"D:\LogS\Logs\";

public static string FloderPath
{
get
{
return logPath;
}
set
{
logPath = value;
}
}

private static object lockHelper = new object();


//简单的写了一个页面

public ActionResult Logtext()
{
return View();
}

//简单的写了一个页面入口提交文字
public ActionResult Add(string Text)
{
string textlog = Text;
//写入
Write("测试:", textlog);
string fileName = "测试:" + "-" + DateTime.Now.ToString("yyyy_MM_dd") + ".log";
//读取
var str = Read(fileName, "utf-8");

var Temp = new
{
nsuccess = false,
message = "写入读取成功"
};
return Json(Temp);
}


/// <summary>
/// 写日志
/// </summary>
/// <param name="LogType">日志类型</param>
/// <param name="Strings">消息</param>
public static bool Write(string LogType, string str)
{
try
{
//string fileName = LogType + "-" + DateTime.Now.ToString("yyyy_MM_dd") + ".log";
if (!System.IO.Directory.Exists(FloderPath))
{
System.IO.Directory.CreateDirectory(FloderPath);
}
return Write(LogType, str, "utf-8");
}
catch
{
return false;
}
}

/// <summary>
/// 写日志gb2312 UTF-8
/// </summary>
/// <param name="LogType">日志类型</param>
/// <param name="str">消息</param>
/// <param name="encoding">编码gb2312 UTF-8</param>
public static bool Write(string LogType, string str, string encoding)
{
if (!System.IO.Directory.Exists(FloderPath))
{
System.IO.Directory.CreateDirectory(FloderPath);
}
string fileName = LogType + "-" + DateTime.Now.ToString("yyyy_MM_dd") + ".log";
bool _isTrue = false;
lock (lockHelper)
{
System.IO.FileStream f = null;
System.IO.StreamWriter f2 = null;
try
{
if (!System.IO.File.Exists(logPath + fileName)) { f = System.IO.File.Create(logPath + fileName); f.Close(); f.Dispose(); f = null; }
f2 = new System.IO.StreamWriter(logPath + fileName, true, System.Text.Encoding.GetEncoding(encoding));
f2.WriteLine("----------------------------------------header-------------------------------------");
f2.WriteLine(str);
f2.WriteLine("----------------------------------------footer-------------------------------------");
_isTrue = true;
}
catch { }
finally
{
if (f != null) { f.Close(); f.Dispose(); f = null; }
if (f2 != null) { f2.Close(); f2.Dispose(); f2 = null; }
}
}
return _isTrue;
}


/// <summary>
/// 读取文件中的内容
/// </summary>
/// <param name="fileName">文件</param>
/// <param name="encoding">编码gb2312 UTF-8</param>
/// <returns>ArrayList</returns>
public static ArrayList Read(string fileName, string encoding)
{
string lineText = null; ArrayList txtTextArr = new ArrayList();
if (!System.IO.File.Exists(FloderPath + fileName)) { txtTextArr = null; return txtTextArr; }

lock (lockHelper)
{
StreamReader reader = encoding.Equals("") ? new StreamReader(FloderPath + fileName) : new StreamReader(FloderPath + fileName, System.Text.Encoding.GetEncoding(encoding));
while ((lineText = reader.ReadLine()) != null)
{
txtTextArr.Add(lineText);
}

reader.Close();
reader.Dispose();
}
return txtTextArr;
}

文件 日志 写入 与读取

标签:line   datetime   odi   lin   cat   ext   head   ret   obj   

原文地址:https://www.cnblogs.com/CSYgo/p/8657682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!