VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之文件 日志 写入 与读取

  • 2019-01-20 15:14 来源:未知

 

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;
}

 

相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号