class ErrorLog
{
/// <summary>
/// 错误编号
/// </summary>
public string ErrorCode { set; get; }
/// <summary>
/// 创建人
/// </summary>
public string Creator { set; get; }
/// <summary>
/// 创建时间
/// </summary>
public string CreateTime { set; get; }
/// <summary>
/// 错误方法名和描述
/// </summary>
public string FunctionName { set; get; }
/// <summary>
/// 错误内容
/// </summary>
public string Content { set; get; }
/// <summary>
/// 写入日志
/// </summary>
/// <param name="errorLog">日志类</param>
public static void AddLog(ErrorLog errorLog)
{
bool _IsFileOrCode = false;//是否有编号txt文件或者是否编号txt文件有传入的错误编号
try
{
StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\ErrorLog\\ErrorCode.txt", Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
if (errorLog.ErrorCode == (line != "" ? line.Split(':')[0] : ""))
{
_IsFileOrCode = true;//有编号了
break;
}
}
sr.Close();
sr.Dispose();
}
catch { } //文件都没得
try
{
string fileName = DateTime.Now.ToString("yyyy-MM-dd");
string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\ErrorLog";
if (Directory.Exists(filePath) == false)
Directory.CreateDirectory(filePath);
string fileAbstractPath = filePath + "\\" + fileName + ".txt";
FileStream fs = new FileStream(fileAbstractPath, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
//开始写入
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
sw.Write("\r\n" + JsonConvert.SerializeObject(errorLog) + "\r\n");
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
if (!_IsFileOrCode)
{
FileStream fs1 = new FileStream(filePath + "\\" + "ErrorCode" + ".txt", FileMode.Append);
StreamWriter sw1 = new StreamWriter(fs1);
sw1.Write("\r\n" + errorLog.ErrorCode + ":" + errorLog.FunctionName);
sw1.Flush();
sw1.Close(); sw1.Dispose();
fs1.Close();
fs1.Dispose();
}
}
catch (Exception ex) { }
}
}
{
/// <summary>
/// 错误编号
/// </summary>
public string ErrorCode { set; get; }
/// <summary>
/// 创建人
/// </summary>
public string Creator { set; get; }
/// <summary>
/// 创建时间
/// </summary>
public string CreateTime { set; get; }
/// <summary>
/// 错误方法名和描述
/// </summary>
public string FunctionName { set; get; }
/// <summary>
/// 错误内容
/// </summary>
public string Content { set; get; }
/// <summary>
/// 写入日志
/// </summary>
/// <param name="errorLog">日志类</param>
public static void AddLog(ErrorLog errorLog)
{
bool _IsFileOrCode = false;//是否有编号txt文件或者是否编号txt文件有传入的错误编号
try
{
StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\ErrorLog\\ErrorCode.txt", Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
if (errorLog.ErrorCode == (line != "" ? line.Split(':')[0] : ""))
{
_IsFileOrCode = true;//有编号了
break;
}
}
sr.Close();
sr.Dispose();
}
catch { } //文件都没得
try
{
string fileName = DateTime.Now.ToString("yyyy-MM-dd");
string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\ErrorLog";
if (Directory.Exists(filePath) == false)
Directory.CreateDirectory(filePath);
string fileAbstractPath = filePath + "\\" + fileName + ".txt";
FileStream fs = new FileStream(fileAbstractPath, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
//开始写入
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
sw.Write("\r\n" + JsonConvert.SerializeObject(errorLog) + "\r\n");
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
if (!_IsFileOrCode)
{
FileStream fs1 = new FileStream(filePath + "\\" + "ErrorCode" + ".txt", FileMode.Append);
StreamWriter sw1 = new StreamWriter(fs1);
sw1.Write("\r\n" + errorLog.ErrorCode + ":" + errorLog.FunctionName);
sw1.Flush();
sw1.Close(); sw1.Dispose();
fs1.Close();
fs1.Dispose();
}
}
catch (Exception ex) { }
}
}