C# 后台添加Log信息
2017-06-16 12:02
253 查看
我们在做项目的时候,经常会使用到Log日志,今天分享一下如何在后台添加Log信息
创建一个写Log的方法:
1 public void WriteLog(string Action) 2 { 3 try 4 { 5 string strLogPath = System.Configuration.ConfigurationManager.AppSettings["LogPath"].ToString(); 6 string strLogName = System.DateTime.Now.ToString("yyyy/MM/dd").Replace("/", "-"); 7 string strIP = Request.UserHostAddress + ":" + Request.Url.Port;//訪問者的Ip和端口 8 if (!strLogPath.EndsWith("\\")) 9 strLogPath += "\\"; 10 strLogPath += "Log\\"; 11 //判斷是否有這樣的路徑并創建 12 if (System.IO.Directory.Exists(strLogPath) == false) 13 { 14 System.IO.Directory.CreateDirectory(strLogPath); 15 } 16 strLogName = strLogPath + strLogName + ".txt"; 17 ////如果文件不存在,會自動創建 18 string strNote = System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); 19 strNote += ":" + strIP + ":\n" + Action + "\n\n"; 20 System.IO.StreamWriter file = new System.IO.StreamWriter(strLogName, true); 21 file.WriteLine(strNote); 22 file.Close(); 23 file.Dispose(); 24 } 25 catch //(Exception ex) 26 { 27 28 } 29 }
调用事件:
1 protected void ibtnQuery_Click(object sender, EventArgs e) 2 { 3 try 4 { 5 Query(); 6 } 7 catch (Exception ex) 8 { 9 WriteLog(ex.Message); 10 } 11 }
效果展示:
相关文章推荐
- C#给图片添加版权信息
- 给图片添加版权信息(C#)
- C# 动态添加SEO 信息,不和静态页面重复和叠加
- 如何在android 系统 C/C++ 层中添加 log 信息
- Metro开发中,C#的工程如何打印它所包含的c++的dll的log信息
- 给图片添加版权信息(C#) [转帖]
- C# 后台动态添加标签(span,div) 以及模板添加
- jquery 结合C#后台的数组对文章的关键字自动添加链接的代码
- jquery 结合C#后台的数组对文章的关键字自动添加链接
- C# Winform打包部署时添加注册表信息实现开机启动
- .net 2008中在html源码中的《script》可以添加c#后台的源代码。下面是MSDN中的例子
- 关于ExtJS的工具栏动态添加按钮(从后台数据库读取信息)
- c#中listview控件添加信息例程
- 二进制图片信息存入数据库并用gridview显示图片(添加鼠标移到图片上显示大图效果)后台语言vb.net
- C# Winform打包部署时添加注册表信息实现开机启动
- C#给图片添加版权信息
- C#在后台添加keywords和description
- C#中为自己的代码自动添加版权信息
- .net c# 用代码来添加断点并且在Visual Studio输出窗口中显示自定义信息
- ecshop后台添加一个完整的统计用户信息的功能