您的位置:首页 > 编程语言 > C#

C#日志方法

2017-09-01 11:51 295 查看
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XSKZQ.Log
{
public class XKLog
{
//private static string FileDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\log\\";
private static string FileDir = System.IO.Path.GetDirectoryName(@"/SDMemory/") + "\\log\\";
private static readonly object log_Lock = new object();
/// <summary>
/// 显控日志写入
/// </summary>
/// <param name="log"></param>
public void WriteLog(string log)
{
if (!Directory.Exists(FileDir)) Directory.CreateDirectory(FileDir);
string FileName = FileDir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
FileName = FileName.Remove(0, 1);
try
{
lock (log_Lock)
{
using (StreamWriter sw = File.AppendText(FileName))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  -->  " + log);
}
}
}
catch (Exception ex)
{
//记录异常
string startError = "XKLog.WriteLog函数异常发生时间:" + DateTime.Now.ToString("[HHmmss]") + " " + ex.InnerException.ToString();
//因为无法预知的异常,并且在出现异常的时候显控无法在窗体上提示,所以我们要创建一个异常文件,来记录当前发生的异常;
string Errorurl = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\ModError.txt";
Errorurl = Errorurl.Replace("file:\\", "");
StreamWriter stream = new StreamWriter(Errorurl, true);
stream.WriteLine(startError);
stream.Flush();
stream.Close();
}
}
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: