0306数据备份整理-【系统日志】C#代码
系统日志
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace BackUp
{
public class Logs
{
private StreamWriter swWrite = null;//写文件
private string logpath = null;//日志路径
/// <summary>
/// 判断是否有当天日志,如果没有则创建
/// </summary>
/// <param name="sCurDate">日期</param>
/// <param name="pathweb">路径</param>
public Logs(string path)
{
logpath = path + DateTime.Now.ToString("yyyyMMdd") + ".log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (!File.Exists(logpath))
{
swWrite = File.CreateText(logpath);
}
else
{
swWrite = File.AppendText(logpath);
}
}
//清单日志
public void LogFileList(string filename, string status, Exception ex)
{
DateTime dt = DateTime.Now;
string sCurDate = dt.ToString("yyyy-MM-dd HH:mm:ss");
if (swWrite!= null)
{
swWrite.WriteLine(sCurDate + " " + filename + " " + status);
//异常记录
if (ex != null)
{
swWrite.WriteLine("异常信息:\r\n" + ex.ToString());
//swWrite.WriteLine("异常堆栈:\r\n" + ex.StackTrace);
}
}
swWrite.WriteLine("\r\n");
swWrite.Flush();
}
//关闭读写流
public void CloseStream()
{
if (swWrite != null)
swWrite.Dispose();
}
}
}