首页 > 代码库 > Logger日志打印普通方法
Logger日志打印普通方法
using System;using System.IO;using System.Text;namespace Core{ public class LogHelper { private static void writeLog(string log) { string strFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs\\log.txt"; string strDirPath = Path.GetDirectoryName(strFilePath); if (!Directory.Exists(strDirPath))//Directory.Exists(dirPath)目录的路径 Directory.CreateDirectory(strDirPath);//Directory.CreateDirectory(path)创建目录 strFilePath = Path.Combine(strDirPath, "log" + string.Format("{0:yyyyMMdd}", DateTime.Now) + ".txt");//Path.Combine(strpath1,strpath2) if (!File.Exists(strFilePath))//File.Exists(path),指定路径的文件是否存在 { //FileStream fsCreate = File.Create(strFilePath);//创建路径 FileStream fsCreate = new FileStream(strFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); fsCreate.Close(); } FileStream fs = new FileStream(strFilePath, FileMode.Append,FileAccess.Write);//任何读取操作都会报异常 StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.WriteLine("--------------------------------------------"); sw.WriteLine("Timestamp: "+DateTime.Now.ToString()); sw.WriteLine("Message: " + log); sw.WriteLine("--------------------------------------------"); sw.Close(); fs.Close(); } //打印普通信息 public static void WriteInfo(string objectName, string info) { writeLog(objectName + ":" + info); } public static void WriteInfo(string objectName, string info, int lines) { string strlines = ""; for (int i = 0; i < lines; i++) { strlines += "\r\n"; } writeLog(objectName+":"+info); //writeLog(strlines + DateTime.Now.ToString() + " INFO " + objectName + " " + info); } //打印错误信息 public static void WriteError(string objectName, string error) { writeLog(objectName+":"+error); //writeLog(DateTime.Now.ToString() + " ERROR " + objectName + " " + error); } public static void WriteError(string objectName, string error, int lines) { string strlines = ""; for (int i = 0; i < lines; i++) { strlines += "\r\n"; } writeLog(objectName + ":" + error); //writeLog(strlines + DateTime.Now.ToString() + " ERROR " + objectName + " " + error); } }}
Logger日志打印普通方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。