ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:78.17KB ,
资源ID:14593194      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-14593194.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#日志记录设计与实现.docx)为本站会员(b****5)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

C#日志记录设计与实现.docx

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