using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SyTech.Log
{
public interface ILogRepository
{
/// <summary>
/// 记录日志
/// </summary>
/// <param name="errorDate">出错时间</param>
/// <param name="source">错误来源</param>
/// <param name="info">异常信息</param>
/// <param name="type">错误类型(ex.Type())</param>
/// <param name="stackTrace">堆栈信息</param>
void Log(DateTime errorDate, string source, string info, string type, string stackTrace);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
namespace SyTech.Log
{
/// <summary>
/// 日志实现
/// </summary>
public class EFLogRepository : ILogRepository
{
/// <summary>
/// 记录日志
/// </summary>
/// <param name="errorDate">出错时间</param>
/// <param name="source">错误来源</param>
/// <param name="info">异常信息</param>
/// <param name="type">错误类型(ex.Type())</param>
/// <param name="stackTrace">堆栈信息</param>
public void Log(DateTime errorDate, string source, string info, string type, string stackTrace)
{
// 生成日志字符串
string strlogJson = "\"errorDate\":\"{0}\",\"writeDate\":\"" + DateTime.Now.ToString() + "\",\"type\":\"{1}\",\"source\":\"{2}\",\"info\":\"{3}\",\"stackTrace\":\"{4}\"";
strlogJson = String.Format(strlogJson, errorDate.ToString(), string.IsNullOrEmpty(type) ? "NULL" : type, string.IsNullOrEmpty(source) ? "NULL" : source, string.IsNullOrEmpty(info) ? "NULL" : info, string.IsNullOrEmpty(stackTrace) ? "NULL" : stackTrace);
// 写日志文件操作
WriteLog("{" + strlogJson + "}" + "\r\n");
}
#region helper
/// <summary>
/// 写日志文件操作
/// </summary>
/// <param name="logstr"></param>
private static void WriteLog(string logstr)
{
FileStream fs = new FileStream(GetFullPath(), FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.Write(logstr);
sw.Close();
fs.Close();
}
/// <summary>
/// 获取日志的全路径
/// </summary>
/// <returns></returns>
private static string GetFullPath()
{
//日志文件的路径
var path = Path.Combine(ConfigurationManager.AppSettings["LogPath"].ToString());
//日志文件的文件名
var fileName = "SyTech_Log" + DateTime.Now.ToString("_yyMMdd") + ".log";
//如果路径不存在则创建
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path + fileName;
}
#endregion
}
}
<asp:Button ID="Button2" runat="server" Text="写日志" OnClick="Button2_Click" />
<asp:Button ID="Button1" runat="server" Text="读日志文件名" OnClick="Button1_Click" />
<asp:Button ID="Button3" runat="server" Text="读文件名内容" OnClick="Button3_Click" />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SyTech.Log;
using System.Configuration;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
namespace SyTech.Test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Log.EFLogRepository log = new EFLogRepository();
log.Log(DateTime.Now, "source", "info", "type", "stackTrace");
}
protected void Button1_Click(object sender, EventArgs e)
{
var path = Path.Combine(ConfigurationManager.AppSettings["LogPath"].ToString());
List<string> fileName = new List<string>();
string[] fn = Directory.GetFiles(path);
foreach (string s in fn)
{
fileName.Add(s.Substring(s.LastIndexOf('\\') + 1));
}
}
protected void Button3_Click(object sender, EventArgs e)
{
var path = Path.Combine(ConfigurationManager.AppSettings["LogPath"].ToString());
var fileName = path + "SyTech_Log_131204.log";
FileStream fs = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(fs);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = sr.ReadToEnd();
string[] ReadText = strLine.Replace("\r\n", "@").Split('@');
List<log> loglst = new List<log>();
foreach (var rt in ReadText)
{
if (string.IsNullOrEmpty(rt))
{
continue;
}
//反序列化
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(rt)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(log));
loglst.Add((log)serializer.ReadObject(ms));
}
}
sr.Close();
fs.Close();
}
public class log
{
public string errorDate { get; set; }
public string writeDate { get; set; }
public string type { get; set; }
public string source { get; set; }
public string info { get; set; }
public string stackTrace { get; set; }
}
}
}