当前位置: 移动技术网 > IT编程>开发语言>.net > 后台日志管理和网站信息设计

后台日志管理和网站信息设计

2018年04月03日  | 移动技术网IT编程  | 我要评论

忍够体,电视机,假期实践心得

后台日志设计

作为一个完善的网站系统,系统日志是必不可少的,常用的组件Log4net就很不错,不过,这些并不适合后台的简要日志,用来记录比如说,系统登录操作的日志,当然也可以,不过保存在文本中是一个很坑爹的事情,我之前也动态配置过Log4net,一个用来记录异常信息,一个用来后台操作信息,不过,看了一些资料,把后台操作信息写在数据库也是一个不错的方案.

可能有人会问后台操作信息有什么用?

当然有用,比如,系统管理员可以很轻松的知道网站管理员最近干了什么,比如xxx偷偷登陆系统删除一些资料的信息,我们就可以很轻松的跟踪管理员的信息,甚至是ip,让那些异常的ip都现形.哈哈哈!

 

要想设计一个日志管理,我们需要建立一个数据库

 


 

大致记录管理员登录的信息,然后开始设计
主要思想:
就是管理员操作的时候就把信息插入数据库,
当然管理日志信息,只能删除三天以前的,也就是说日志强制保留三天.
这里用的是JQuery MINIUI,
本来以前是打算用EasyUI做后台的,不过demo实例,不像MINIUI那么完整,
EasyUI中文教程蛮多的
这只是一个初步的雏形,还在慢慢修改!
前台页面大致代码:
   1:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
   2:  <html xmlns="">
   3:  <head>
   4:      <title>日志信息管理</title>
   5:      <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
   6:      <link href="../css/demo.css" rel="stylesheet" type="text/css" />
   7:  
   8:      <script src="../scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
   9:  
  10:      <script src="../scripts/miniui/miniui.js" type="text/javascript"></script>
  11:  
  12:      <link href="../scripts/miniui/themes/default/miniui.css" rel="stylesheet" type="text/css" />
  13:      <link href="../scripts/miniui/themes/icons.css" rel="stylesheet" type="text/css" />
  14:  </head>
  15:  <body>
  16:      <p class="mini-toolbar">
  17:          <h1>
  18:              日志信息管理</h1>
  19:          <a class="mini-button" iconcls="icon-remove" onclick="remove">清空日志</a>
  20:          (默认日志保留三天,只能清空三天前的所有日志)
  21:          <span class="separator">
  22:           关键字:
  23:          <input class="mini-textbox" id="key" name="key" type="text" />
  24:          <span class="separator"></span><a class="mini-button" iconcls="icon-search" id="search">
  25:              查询</a>
  26:      </p>
  27:      <p id="datagrid1" class="mini-datagrid" style="width: 100%; height: 400px;" allowresize="true"
  28:          url="Data/GetOurInfo.ashx?method=SearchSystemLog" idfield="Id" multiselect="false">
  29:          <p property="columns">
  30:              <!--<p type="indexcolumn"></p>        -->
  31:              <p type="checkcolumn">
  32:              </p>
  33:              <p id="deal" name="action" width="100" headeralign="center" align="center" renderer="onActionRenderer"
  34:                  cellstyle="padding:0;">
  35:                  操作
  36:              </p>
  37:              <p field="UserName" width="100" headeralign="center" allowsort="true">
  38:                  管理员</p>
  39:              <p field="Url" width="140">
  40:                  ip地址</p>
  41:              <p field="Title" width="400">
  42:                  日志</p>
  43:              <p field="AddTime" width="100" renderer="onAddTimeRenderer" >
  44:                  访问时间</p>
  45:          </p>
  46:      </p>
  47:  
  48:      <script type="text/javascript">
  49:          mini.parse();
  50:  
  51:          var grid = mini.get("datagrid1");
  52:        //  grid.load();
  53:          grid.sortBy("AddTime", "desc");
  54:      
  55:          function remove(e) {
  56:                  if (confirm("确定删除三天前的日志记录?")) {
  57:                      grid.loading("操作中,请稍后......");
  58:                      $.ajax({
  59:                          url: "Data/GetOurInfo.ashx?method=RemoveSystemLog",
  60:                          success: function (text) {
  61:                              grid.reload();
  62:                          },
  63:                          error: function () {
  64:                          }
  65:                      });
  66:                  }
  67:            
  68:          }
  69:          function search(e) {
  70:           var  key= mini.get("key");
  71:          grid.load({ key: key.value });
  72:          }
  73:          $("#search").bind("click",function(e)
  74:          {
  75:          search(e);
  76:          });
  77:          $("#key").bind("keydown", function (e) {
  78:              if (e.keyCode == 13) {
  79:                  search(e);
  80:              }
  81:          });
  82:          /////////////////////////////////////////////////
  83:          function onAddTimeRenderer(e) {
  84:              var value = e.value;
  85:              if (value) return mini.formatDate(value, 'yyyy-MM-dd hh:mm');
  86:              return "";
  87:          }
  88:        
  89:          grid.set({ footerStyle: "padding-right:10px;" });
  90:  
  91:  
  92:  
  93:      </script>
  94:  
  95:  </body>
  96:  </html>
后台大致的代码:
 
   1:      /// <summary>
   2:      /// 删除系统日志
   3:      /// </summary>
   4:      /// <param name="context">删除系统日志</param>
   5:      public void RemoveSystemLog(HttpContext context)
   6:      {
   7:          //清空三天前的日志
   8:         context.Response.Write(new SystemLogBLL().CearSystemLog());
   9:      }
  10:      /// <summary>
  11:      /// 获取系统日志信息
  12:      /// </summary>
  13:      /// <param name="context"></param>
  14:      public void SearchSystemLog(HttpContext context)
  15:      {
  16:          //查询条件
  17:          string key = context.Request["key"];
  18:          //分页
  19:          int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
  20:          int pageSize = Convert.ToInt32(context.Request["pageSize"]);
  21:          //字段排序
  22:          String sortField = context.Request["sortField"];
  23:          String sortOrder = context.Request["sortOrder"];
  24:          string strCondition = "";
  25:          //对搜索内容进行验证
  26:          if (!Common.Tools.IsValidInput(ref key, false))
  27:          {
  28:              return;
  29:          }
  30:          else
  31:              strCondition =SystemLogBLL.ConfirmCondition(key);//判断查询条件
  32:          SystemLogBLL bll = new SystemLogBLL();
  33:          //分页数据读取
  34:          IEnumerable<SystemLog> list = bll.ListByPagination(sortField, pageSize, pageIndex + 1, sortOrder == "asc" ? "0" : "1", strCondition);
  35:          //获取总页数
  36:          int totalPage = bll.GetCount(strCondition);
  37:          //JSON 序列化 www.2cto.com
  38:          string json = SystemLogBLL.MiniUiListToJson(list, totalPage, "");
  39:  
  40:  
  41:          context.Response.Write(json);
  42:      }

 
 BLL主要操作:


这样一个日志管理就完成了!

接下来,我们要做的就是一个关于网站信息的内容,主要是显示这个网站信息,公司等等
基本上都是读取数据库信息,不过作为一个专业的网站,感觉这个其实很重要,虽然这是一个非常简单的东西,不过这确是一个网站必须有的东西!

当然代码就非常简单了,主要就是读取信息!
 

 

主要json数据生成:
   1:    /// <summary>
   2:          /// 关于网站信息的的json格式
   3:          /// </summary>
   4:          /// <param name="user"></param>
   5:          /// <returns></returns>
   6:          public static string OurWebInfoToJson(ourinfo Info)
   7:          {
   8:              //服务器名称
   9:             string MachineName=HttpContext.Current.Server.MachineName;
  10:             //服务器ip
  11:              string LocalIp = HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
  12:              //.net版本
  13:              string NetFramework = Environment.Version.ToString();
  14:              //操作系统版本
  15:              string OSVersion = Environment.OSVersion.ToString();
  16:              //iis版本
  17:              string IISVersion = HttpContext.Current.Request.ServerVariables["SERVER_SOFTWARE"];
  18:              //服务器端口
  19:              string ServerPort = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
  20:              //虚拟目录绝对路径
  21:              string RealDirectory = HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
  22:              //https支持
  23:              string SupportHttps = HttpContext.Current.Request.ServerVariables["HTTPS"];  
  24:              //session总数
  25:              string SessionCount = HttpContext.Current.Session.Keys.Count.ToString();
  26:  
  27:              StringBuilder Json = new StringBuilder();
  28:              StringWriter sw = new StringWriter(Json);
  29:              using (JsonWriter jsonWriter = new JsonTextWriter(sw))
  30:              {
  31:  
  32:                  jsonWriter.Formatting = Formatting.Indented;
  33:  
  34:                  jsonWriter.WriteStartObject();
  35:                  //网站信息
  36:                  jsonWriter.WritePropertyName("MachineName");
  37:                  jsonWriter.WriteValue(MachineName);
  38:                  jsonWriter.WritePropertyName("LocalIp");
  39:                  jsonWriter.WriteValue(LocalIp);
  40:                  jsonWriter.WritePropertyName("NetFramework");
  41:                  jsonWriter.WriteValue(NetFramework);
  42:                  jsonWriter.WritePropertyName("OSVersion");
  43:                  jsonWriter.WriteValue(OSVersion);
  44:                  jsonWriter.WritePropertyName("IISVersion");
  45:                  jsonWriter.WriteValue(IISVersion);
  46:                  jsonWriter.WritePropertyName("ServerPort");
  47:                  jsonWriter.WriteValue(ServerPort);
  48:                  jsonWriter.WritePropertyName("RealDirectory");
  49:                  jsonWriter.WriteValue(RealDirectory);
  50:                  jsonWriter.WritePropertyName("SupportHttps");
  51:                  jsonWriter.WriteValue(SupportHttps);
  52:                  jsonWriter.WritePropertyName("SessionCount");
  53:                  jsonWriter.WriteValue(SessionCount);
  54:  
  55:                  //公司信息
  56:                  jsonWriter.WritePropertyName("id");
  57:                  jsonWriter.WriteValue(Info.Id);
  58:                  jsonWriter.WritePropertyName("Name");
  59:                  jsonWriter.WriteValue(Info.Name);
  60:                  jsonWriter.WritePropertyName("Introduction");
  61:                  jsonWriter.WriteValue(Info.Introduction);
  62:                  jsonWriter.WritePropertyName("Representative");
  63:                  jsonWriter.WriteValue(Info.Representative);
  64:                  jsonWriter.WritePropertyName("Website");
  65:                  jsonWriter.WriteValue(Info.Website);
  66:                  jsonWriter.WritePropertyName("mobilephone");
  67:                  jsonWriter.WriteValue(Info.mobilephone);
  68:                  jsonWriter.WritePropertyName("Telephone");
  69:                  jsonWriter.WriteValue(Info.Telephone);
  70:                  jsonWriter.WritePropertyName("Email");
  71:                  jsonWriter.WriteValue(Info.Email);
  72:                  jsonWriter.WritePropertyName("qq");
  73:                  jsonWriter.WriteValue(Info.qq);
  74:                  jsonWriter.WritePropertyName("Zipcode");
  75:                  jsonWriter.WriteValue(Info.Zipcode);
  76:                  jsonWriter.WritePropertyName("Address");
  77:                  jsonWriter.WriteValue(Info.Address);
  78:  
  79:                  jsonWriter.WriteEndObject();
  80:  
  81:  
  82:              }
  83:              return Json.ToString();
  84:  
  85:          }
这样就完成了,其实都是简单的模块,但是这些细节是网站一定要做的地方.以前,做东西,太粗糙,感觉拿不出手,很多东西不考虑!
一步一步学习,细节决定成败!
一直在努力,从未曾放弃,努力学习中..... 欢迎一起学习.net!

 

摘自 TZHSWEET

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网