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(); } } } }
相关文章推荐
- C#在服务器上调试程序的方法(生成日志文件在服务器中)
- C#写日志两个简单方法
- [C#]log4net写SQLServer数据库日志的配置方法-.NET教程,C#语言
- 一段c#写的记录日志方法,分月分日记录
- C#写日志方法
- c#写日志方法
- C#记录消息到日志文件的方法
- .Net下几种日志管理方法(C# 将Diagnostics.Trace 调试输出 保存到txt)
- [C#]log4net写SQLServer数据库日志的配置方法
- C#写日志 方法
- c#快速写本地日志方法
- c# 一个记录日志的通用方法
- 分享一篇关于C#对文件操作的日志,方法很全
- C#中简单的写日志的方法
- C# 类静态实例日志方法
- C#实现线程安全的简易日志记录方法
- C#中错误日志记录方法,信息比较详细易定位
- 分享一篇关于C#对文件操作的日志,方法很全
- [C#]log4net写SQLServer数据库日志的配置方法
- C#中四步轻松使用log4net记录本地日志的方法