1、C#日志记录设计与实现C#日志记录设计与实现日志记录:日志记录在程序设计开发过程中,是非常重要的,可以供调试和记录数据,虽然说有开源的强大日志管理系统,比如apache的Log4Net,功能可谓强悍,但是有时候,不需要这么大的日志,只需要显示和文件记录就可以了,没必要用这么重的日志系统,那么就需要自己来写,如下就是一个简单的日志记录和显示模块的设计和实现,如有不足,还望见谅!废话不多,直入主题。笨小孩日志:BenXHLog类文件设计:文件结构简单,类图就不画了,细心的已经发现了,这就是一个简单工厂模式,程序代码:Ilog接口 1 using System; 2 using System.Col
2、lections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace BenXH.Log.Log 7 8 public interface ILog 9 10 bool IsDebugEnabled get; 11 12 bool IsErrorEnabled get; 13 14 bool IsFatalEnabled get; 15 16 bool IsInfoEnabled get; 17 18 bool IsWarnEnabled get; 19 20 void Debug(bool isWriteFile,
3、object message);21 22 void Debug(bool isWriteFile, object message, Exception exception);23 24 void DebugFormat(bool isWriteFile, string format, object arg0);25 26 void DebugFormat(bool isWriteFile, string format, params object args);27 28 void DebugFormat(bool isWriteFile, IFormatProvider provider,
4、string format, params object args);29 30 void DebugFormat(bool isWriteFile, string format, object arg0, object arg1);31 32 void DebugFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);33 34 void Error(bool isWriteFile,object message);35 36 void Error(bool isWriteFile, obj
5、ect message, Exception exception);37 38 void ErrorFormat(bool isWriteFile, string format, object arg0);39 40 void ErrorFormat(bool isWriteFile, string format, params object args);41 42 void ErrorFormat(bool isWriteFile, IFormatProvider provider, string format, params object args);43 44 void ErrorFor
6、mat(bool isWriteFile, string format, object arg0, object arg1);45 46 void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);47 48 void Fatal(bool isWriteFile, object message);49 50 void Fatal(bool isWriteFile, object message, Exception exception);51 52 void FatalFor
7、mat(bool isWriteFile, string format, object arg0);53 54 void FatalFormat(bool isWriteFile, string format, params object args);55 56 void FatalFormat(bool isWriteFile, IFormatProvider provider, string format, params object args);57 58 void FatalFormat(bool isWriteFile, string format, object arg0, obj
8、ect arg1);59 60 void FatalFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);61 62 void Info(bool isWriteFile, object message);63 64 void Info(bool isWriteFile, object message, Exception exception);65 66 void InfoFormat(bool isWriteFile, string format, object arg0);67 68
9、void InfoFormat(bool isWriteFile, string format, params object args);69 70 void InfoFormat(bool isWriteFile, IFormatProvider provider, string format, params object args);71 72 void InfoFormat(bool isWriteFile, string format, object arg0, object arg1);73 74 void InfoFormat(bool isWriteFile, string fo
10、rmat, object arg0, object arg1, object arg2);75 76 void Warn(bool isWriteFile, object message);77 78 void Warn(bool isWriteFile, object message, Exception exception);79 80 void WarnFormat(bool isWriteFile, string format, object arg0);81 82 void WarnFormat(bool isWriteFile, string format, params obje
11、ct args);83 84 void WarnFormat(bool isWriteFile, IFormatProvider provider, string format, params object args);85 86 void WarnFormat(bool isWriteFile, string format, object arg0, object arg1);87 88 void WarnFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);89 90 ILogFacto
12、ry工厂接口using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace BenXH.Log.Log public interface ILogFactory ILog GetLog(string name); 日志类Log 这个代码实现多一点,合并了,点开看吧View CodeLogFactory 日志工厂using System;using System.Collections.Generic;using System.Linq;using System.Text;na
13、mespace BenXH.Log.Log public class LogFactory:ILogFactory / / 创建日志实例 / / / public ILog GetLog(string name) return new Log(name); LogUtil日志格式化 1 using BenXH.Log.Common; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 7 namespace BenXH.Log.Log 8 9 intern
14、al class LogUtil10 11 / 12 / 格式式化Log信息13 / 14 / 15 / 16 / 17 / 18 / 19 private static string GetLogString(string name, string logType, string log)20 21 return String.Format(01-2: 3, DateTime.Now.ToString(HH:mm:ss),name, logType, log);22 23 24 / 25 / 获得日志要保存的路径26 / 27 / 28 / 29 / 30 private static st
15、ring GetLogPath(string name, string logType)31 32 string path = AppDomain.CurrentDomain.BaseDirectory+Log;33 if (!System.IO.Directory.Exists(path)34 35 System.IO.Directory.CreateDirectory(path);36 37 38 return System.IO.Path.Combine(path,String.Format(0_1_2.log,DateTime.Now.ToString(yyyy-MM-dd),name
16、,logType);39 40 41 public static void WriteLogFile(string name, string logType, string log)42 43 string logPath = GetLogPath(name, logType);44 45 FileUtil.WriteAppend(logPath,log);46 47 48 最后就是一个日志信息的显示和文件的存储 FileUtil 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using Sys
17、tem.Linq; 5 using System.Text; 6 7 namespace BenXH.Log.Common 8 9 public static class FileUtil10 11 / 12 / 追加内容到指定文件中13 / 14 / 15 / 16 public static void WriteAppend(string filePath, string content)17 18 WriteAppend(filePath,new stringcontent);19 20 21 public static void WriteAppend(string filePath,
18、 string contents)22 23 /System.IO.StreamWriter sr = new System.IO.StreamWriter(filePath, true);24 /foreach (string c in contents)25 /26 / sr.WriteLine(c);27 /28 /sr.Flush();29 /sr.Close();30 31 using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWri
19、te)32 33 fs.Seek(fs.Length, SeekOrigin.Current);34 35 string content = String.Join(Environment.NewLine, contents) + Environment.NewLine;36 37 byte data = System.Text.Encoding.UTF8.GetBytes(content);38 39 fs.Write(data, 0, data.Length);40 41 fs.Close();42 43 44 45 测试代码:Test.cs 1 class Test 2 3 static
20、 void Main(string args) 4 5 /日志BenXH的Debug信息 6 new BenXH.Log.Log.LogFactory().GetLog(BenXH).Debug(true, Hello); 7 new BenXH.Log.Log.LogFactory().GetLog(BenXH).Debug(true, World); 8 9 /日志BenXH的info信息10 new BenXH.Log.Log.LogFactory().GetLog(BenXH).Info(true, Hello);11 new BenXH.Log.Log.LogFactory().GetLog(BenXH).Info(true, BenXH);12 Console.Read();13 14 运行结果:文件:程序运行根目录下Log文件夹Log文件夹下新创建俩个文件DEBUG和INFO文件:2017-07-29_BenXH_DEBUG.log文件内容17-07-29 16:01:26Hello17-07-29 16:01:26World文件:2017-07-29_BenXH_INFO.log文件内容:17-07-29 16:01:26Hello17-07-29 16:01:26BenXH
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2