系列目录

系统需要越来越自动化,我们需要引入日志记录和异常捕获
管理员的操作记录需要被记录,看出哪些模块是频繁操作,分析哪些是不必要的功能,哪些是需要被优化的。
系统的异常需要被捕获,而不是将系统出错显示出来给用户就不了了知。我们需要异常日志不断改进系统。
我们老说用户,我们还没有用户权限的表,所以我们在Home中先加入一个虚拟用户吧!

首先我们创建一个用户类AccountModel放在App.Models下的Sys文件夹下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace App.Models.Sys
  7. {
  8. public class AccountModel
  9. {
  10. public string Id { get; set; }
  11. public string TrueName { get; set; }
  12. }
  13. }

AccountModel.cs

在HomeController或者AccountController插入代码

  1. AccountModel account = new AccountModel();
  2. account.Id = "admin";
  3. account.TrueName = "admin";
  4. Session["Account"] = account;

下面将带来系统日志的记录,主要记录管理员的增、删、改等操作的成功与失败的异常记录
日志插件有著名的log4net,可以输出多种格式,如文本,xml,数据库等,我们没有必要做到这么强大,我们只做符合系统的就可以了,记录到数据库,方便做统计等操
作,我们何时何地记录日志?

  • 在Controller层做记录;
  • 当用户的操作成功时记录;
  • 当用户的操作失败时记录;

首先创建数据库存放表:SysLog

  1. USE DB
  2. GO
  3.  
  4. /****** Object: Table [dbo].[SysLog] Script Date: 11/20/2013 21:13:38 ******/
  5. SET ANSI_NULLS ON
  6. GO
  7.  
  8. SET QUOTED_IDENTIFIER ON
  9. GO
  10.  
  11. SET ANSI_PADDING ON
  12. GO
  13.  
  14. CREATE TABLE [dbo].[SysLog](
  15. [Id] [varchar](50) NOT NULL, --GUID
  16. [Operator] [varchar](50) NULL,--操作人
  17. [Message] [varchar](500) NULL,--操作信息
  18. [Result] [varchar](20) NULL,--结果
  19. [Type] [varchar](20) NULL,--操作类型
  20. [Module] [varchar](20) NULL,--操作模块
  21. [CreateTime] [datetime] NULL,--操作事件
  22. CONSTRAINT [PK_SysLog] PRIMARY KEY CLUSTERED
  23. (
  24. [Id] ASC
  25. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  26. ) ON [PRIMARY]
  27.  
  28. GO
  29.  
  30. SET ANSI_PADDING OFF
  31. GO

SysLogSQL

EF更新模型,创建SysLogModel类放在App.Models下的Sys文件夹下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel.DataAnnotations;
  6.  
  7. namespace App.Models.Sys
  8. {
  9. public class SysLogModel
  10. {
  11.  
  12. [Display(Name = "ID")]
  13. public string Id { get; set; }
  14.  
  15. [Display(Name = "操作人")]
  16. public string Operator { get; set; }
  17.  
  18. [Display(Name = "信息")]
  19. public string Message { get; set; }
  20.  
  21. [Display(Name = "结果")]
  22. public string Result { get; set; }
  23.  
  24. [Display(Name = "类型")]
  25. public string Type { get; set; }
  26.  
  27. [Display(Name = "模块")]
  28. public string Module { get; set; }
  29.  
  30. [Display(Name = "创建时间")]
  31. public DateTime? CreateTime { get; set; }
  32. }
  33. }

SysLogModel.cs

创建SysLog的BLL层和DAL层

  1. using System;
  2. using App.Models;
  3. using System.Linq;
  4. namespace App.IDAL
  5. {
  6. public interface ISysLogRepository
  7. {
  8. int Create(SysLog entity);
  9. void Delete(DBContainer db, string[] deleteCollection);
  10. IQueryable<SysLog> GetList(DBContainer db);
  11. SysLog GetById(string id);
  12. }
  13. }

ISysLogRepository

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using App.Models;
  6. using App.IDAL;
  7.  
  8. namespace App.DAL
  9. {
  10. public class SysLogRepository:IDisposable, ISysLogRepository
  11. {
  12. /// <summary>
  13. /// 获取集合
  14. /// </summary>
  15. /// <param name="db">数据库</param>
  16. /// <returns>集合</returns>
  17. public IQueryable<SysLog> GetList(DBContainer db)
  18. {
  19. IQueryable<SysLog> list = db.SysLog.AsQueryable();
  20. return list;
  21. }
  22. /// <summary>
  23. /// 创建一个对象
  24. /// </summary>
  25. /// <param name="db">数据库</param>
  26. /// <param name="entity">实体</param>
  27. public int Create(SysLog entity)
  28. {
  29. using (DBContainer db = new DBContainer())
  30. {
  31. db.SysLog.AddObject(entity);
  32. return db.SaveChanges();
  33. }
  34.  
  35. }
  36.  
  37. /// <summary>
  38. /// 删除对象集合
  39. /// </summary>
  40. /// <param name="db">数据库</param>
  41. /// <param name="deleteCollection">集合</param>
  42. public void Delete(DBContainer db, string[] deleteCollection)
  43. {
  44. IQueryable<SysLog> collection = from f in db.SysLog
  45. where deleteCollection.Contains(f.Id)
  46. select f;
  47. foreach (var deleteItem in collection)
  48. {
  49. db.SysLog.DeleteObject(deleteItem);
  50. }
  51. }
  52. /// <summary>
  53. /// 根据ID获取一个实体
  54. /// </summary>
  55. /// <param name="id"></param>
  56. /// <returns></returns>
  57. public SysLog GetById(string id)
  58. {
  59. using (DBContainer db = new DBContainer())
  60. {
  61. return db.SysLog.SingleOrDefault(a => a.Id == id);
  62. }
  63. }
  64. public void Dispose()
  65. {
  66.  
  67. }
  68. }
  69. }

SysLogRepository

  1. using System;
  2. using System.Collections.Generic;
  3. using App.Common;
  4. using App.Models;
  5. namespace App.IBLL
  6. {
  7. public interface ISysLogBLL
  8. {
  9. List<SysLog> GetList(ref GridPager pager,string queryStr);
  10. SysLog GetById(string id);
  11. }
  12. }

ISysLogBLL

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Practices.Unity;
  6. using App.IDAL;
  7. using App.Common;
  8. using App.Models.Sys;
  9. using App.Models;
  10. using App.IBLL;
  11. namespace App.BLL
  12. {
  13. public class SysLogBLL: ISysLogBLL
  14. {
  15. [Dependency]
  16. public ISysLogRepository logRepository { get; set; }
  17.  
  18. public List<SysLog> GetList(ref GridPager pager, string queryStr)
  19. {
  20. DBContainer db = new DBContainer();
  21. List<SysLog> query = null;
  22. IQueryable<SysLog> list = logRepository.GetList(db);
  23. if (!string.IsNullOrWhiteSpace(queryStr))
  24. {
  25. list = list.Where(a => a.Message.Contains(queryStr) || a.Module.Contains(queryStr));
  26. pager.totalRows = list.Count();
  27. }
  28. else
  29. {
  30. pager.totalRows = list.Count();
  31. }
  32.  
  33. if (pager.order == "desc")
  34. {
  35. query = list.OrderByDescending(c => c.CreateTime).Skip((pager.page - ) * pager.rows).Take(pager.rows).ToList();
  36. }
  37. else
  38. {
  39. query = list.OrderBy(c => c.CreateTime).Skip((pager.page - ) * pager.rows).Take(pager.rows).ToList();
  40. }
  41.  
  42. return query;
  43. }
  44. public SysLog GetById(string id)
  45. {
  46. return logRepository.GetById(id);
  47. }
  48. }
  49. }

SysLogBLL

创建SysLog的Controller

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. using App.Common;
  8. using App.Models;
  9. using Microsoft.Practices.Unity;
  10. using App.IBLL;
  11. using App.Models.Sys;
  12.  
  13. namespace App.Admin.Controllers
  14. {
  15. public class SysLogController : Controller
  16. {
  17. //
  18. // GET: /SysLog/
  19. [Dependency]
  20. public ISysLogBLL logBLL { get; set; }
  21.  
  22. public ActionResult Index()
  23. {
  24.  
  25. return View();
  26.  
  27. }
  28.  
  29. public JsonResult GetList(GridPager pager, string queryStr)
  30. {
  31. List<SysLog> list = logBLL.GetList(ref pager, queryStr);
  32. var json = new
  33. {
  34. total = pager.totalRows,
  35. rows = (from r in list
  36. select new SysLogModel()
  37. {
  38.  
  39. Id= r.Id,
  40. Operator= r.Operator,
  41. Message= r.Message,
  42. Result= r.Result,
  43. Type= r.Type,
  44. Module= r.Module,
  45. CreateTime= r.CreateTime
  46.  
  47. }).ToArray()
  48.  
  49. };
  50.  
  51. return Json(json);
  52. }
  53.  
  54. #region 详细
  55.  
  56. public ActionResult Details(string id)
  57. {
  58.  
  59. SysLog entity = logBLL.GetById(id);
  60. SysLogModel info = new SysLogModel()
  61. {
  62. Id = entity.Id,
  63. Operator = entity.Operator,
  64. Message = entity.Message,
  65. Result = entity.Result,
  66. Type = entity.Type,
  67. Module = entity.Module,
  68. CreateTime = entity.CreateTime,
  69. };
  70. return View(info);
  71. }
  72.  
  73. #endregion
  74.  
  75. }
  76. }

SysLogController.cs

创建SysLog的Index视图和Details视图,我们暂时提示Index和Details,删除功能童鞋们自己扩展,我们有样例程序SysSample嘛,什么都是必然的了

  1. @using App.Admin;
  2. @using App.Common;
  3. @using App.Models.Sys;
  4.  
  5. @{
  6. ViewBag.Title = "Index";
  7. Layout = "~/Views/Shared/_Index_Layout.cshtml";
  8.  
  9. }
  10. <script src="~/Scripts/jquery.easyui.plus.js"></script>
  11. <div class="mvctool">
  12. <input id="txtQuery" type="text" class="searchText"/>
  13. <a id="btnQuery" style="float: left;" class="l-btn l-btn-plain"><span class="l-btn-left"><span class="l-btn-text icon-search" style="padding-left: 20px;">查询</span></span></a><div class="datagrid-btn-separator"></div>
  14. <a id="btnDetails" style="float: left;" class="l-btn l-btn-plain"><span class="l-btn-left"><span class="l-btn-text icon-details" style="padding-left: 20px;">详细</span></span></a><div class="datagrid-btn-separator"></div>
  15. <a id="btnDelete" style="float: left;" class="l-btn l-btn-plain"><span class="l-btn-left"><span class="l-btn-text icon-remove" style="padding-left: 20px;">删除</span></span></a>
  16. </div>
  17.  
  18. <table id="List"></table>
  19. <div id="Pager"></div>
  20. <div id="modalwindow" class="easyui-window" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
  21. @*Jqgrid*@
  22. <script type="text/javascript">
  23. //ifram 返回
  24. function frameReturnByClose() {
  25. $("#modalwindow").window('close');
  26. }
  27. function frameReturnByReload(flag) {
  28. if (flag)
  29. $("#List").datagrid('load');
  30. else
  31. $("#List").datagrid('reload');
  32. }
  33. function frameReturnByMes(mes) {
  34. $.messageBox5s('提示', mes);
  35. }
  36.  
  37. $(function () {
  38. $('#List').datagrid({
  39. url: '/SysLog/GetList',
  40. width: $(window).width() - ,
  41. methord: 'post',
  42. height: $(window).height() - ,
  43. fitColumns: true,
  44. sortName: 'Id',
  45. sortOrder: 'desc',
  46. idField: 'Id',
  47. pageSize: ,
  48. pageList: [, , , , ],
  49. pagination: true,
  50. striped: true, //奇偶行是否区分
  51. singleSelect: true,//单选模式
  52. columns: [[
  53. { field: 'Id', title: 'ID', width: , hidden: true },
  54. { field: 'Operator', title: '操作人', width: },
  55. { field: 'Message', title: '信息', width: },
  56. { field: 'Result', title: '结果', width: , align: 'center' },
  57. { field: 'Type', title: '类型', width: , align: 'center' },
  58. { field: 'Module', title: '模块', width: , align: 'center' },
  59. { field: 'CreateTime', title: '添加时间', width: , align: 'center' }
  60. ]]
  61. });
  62. });
  63.  
  64. </script>
  65.  
  66. @*operation*@
  67. <script type="text/javascript">
  68. $(function () {
  69. $("#btnDetails").click(function () {
  70. var row = $('#List').datagrid('getSelected');
  71. if (row != null) {
  72.  
  73. $("#modalwindow").html("<iframe width='100%' height='98%' frameborder='0' src='/SysLog/Details?id=" + row.Id + "'></iframe>");
  74. $("#modalwindow").window({ title: '详细', width: , height: , iconCls: 'icon-details' }).window('open');
  75. } else { $.messageBox5s('提示', '请选择要操作的行!'); }
  76. });
  77. $("#btnQuery").click(function () {
  78. var queryStr = $("#txtQuery").val();
  79. //如果查询条件为空默认查询全部
  80. if (queryStr == null) {
  81. queryStr = "%";
  82. }
  83. $('#List').datagrid({ url: '/SysLog/GetList?queryStr=' + encodeURI(queryStr)});
  84. });
  85.  
  86. });
  87. </script>

Index.cshtml

  1. @model App.Models.Sys.SysLogModel
  2. @using App.Common;
  3. @using App.Admin;
  4. @using App.Models.Sys;
  5. @{
  6. ViewBag.Title = "Details";
  7. Layout = "~/Views/Shared/_Index_LayoutEdit.cshtml";
  8.  
  9. }
  10. <script type="text/javascript">
  11. $(function () {
  12. $("#btnReturn").click(function () {
  13. window.parent.frameReturnByClose();
  14. });
  15. });
  16. </script>
  17. <div class="mvctool bgb">
  18. <a id="btnReturn" style="float: left;" class="l-btn l-btn-plain"><span class="l-btn-left"><span class="l-btn-text icon-return" style="padding-left: 20px;">返回</span></span></a>
  19. </div>
  20. @using (Html.BeginForm())
  21. {
  22. <table class="form_table setinput355">
  23. <tbody>
  24. <tr>
  25. <th>
  26. @Html.LabelFor(model => model.Operator)
  27. </th>
  28. <td>
  29. @Html.EditorFor(model => model.Operator)
  30. </td>
  31. </tr>
  32. <tr>
  33. <th>
  34. @Html.LabelFor(model => model.Message)
  35. </th>
  36. <td>
  37. @Html.TextAreaFor(model => model.Message, new { @style="height:100px;"})
  38. </td>
  39. </tr>
  40. <tr>
  41. <th>
  42. @Html.LabelFor(model => model.Result)
  43. </th>
  44. <td>
  45. @Html.EditorFor(model => model.Result)
  46. </td>
  47. </tr>
  48. <tr>
  49. <th>
  50. @Html.LabelFor(model => model.Type)
  51. </th>
  52. <td>
  53. @Html.EditorFor(model => model.Type)
  54. </td>
  55. </tr>
  56. <tr>
  57. <th>
  58. @Html.LabelFor(model => model.Module)
  59. </th>
  60. <td>
  61. @Html.EditorFor(model => model.Module)
  62. </td>
  63. </tr>
  64. <tr>
  65. <th>
  66. @Html.LabelFor(model => model.CreateTime)
  67. </th>
  68. <td>
  69. @Html.TextBoxFor(model => model.CreateTime)
  70. </td>
  71. </tr>
  72. </tbody>
  73. </table>
  74. }

Details.cshtml

有看过前面的童鞋,应该很熟悉这一步很机械化的创建了

  1. 创建数据表
  2. 更新到EF
  3. 创建BLL和DAL层
  4. 创建Model
  5. 创建爱你Controller
  6. 创建View
  7. 注入到容器
  8. 运行

你看了不累我都觉得累了,我们以后会讲用T4,我们自动生成

预览下效果,你会发现我们的左边的菜单栏可以点出来了。oh yeh...(别忘记注入)

分页和详细都没有问题了。

接下来是是异常的捕获,我们在何时处理异常?我们没有处理的异常该怎么办?我们处理异常时出现异常怎么又怎么办?反正我是要捕获到这异常了...、
我们一般先对数据进行判断避免捕获异常,因为try catch会降低程序的性能,我们一般在业务层捕获异常,处理逻辑容易导致异常

  • 处理异常出错,我们将输出文本格式,来记录异常
  • 我们将写全局异常捕获来拦截异常
  • 你觉得我们的系统后盾还不够强大吗?

创建异常存放数据表SysException

  1. USE DB
  2. GO
  3.  
  4. /****** Object: Table [dbo].[SysException] Script Date: 11/20/2013 21:17:44 ******/
  5. SET ANSI_NULLS ON
  6. GO
  7.  
  8. SET QUOTED_IDENTIFIER ON
  9. GO
  10.  
  11. SET ANSI_PADDING ON
  12. GO
  13.  
  14. CREATE TABLE [dbo].[SysException](
  15. [Id] [varchar](50) NOT NULL, --GUID
  16. [HelpLink] [varchar](500) NULL,--帮助链接
  17. [Message] [varchar](500) NULL,--异常信息
  18. [Source] [varchar](500) NULL,--来源
  19. [StackTrace] [text] NULL,--堆栈
  20. [TargetSite] [varchar](500) NULL,--目标页
  21. [Data] [varchar](500) NULL,--程序集
  22. [CreateTime] [datetime] NULL,--发生时间
  23. CONSTRAINT [PK_SysException] PRIMARY KEY CLUSTERED
  24. (
  25. [Id] ASC
  26. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  27. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  28.  
  29. GO
  30.  
  31. SET ANSI_PADDING OFF
  32. GO

SysExceptionSQL

EF更新模型,创建SysExceptionModel类放在App.Models下的Sys文件夹下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel.DataAnnotations;
  6.  
  7. namespace App.Models.Sys
  8. {
  9. /// <summary>
  10. /// 异常处理类
  11. /// </summary>
  12. public class SysExceptionModel
  13. {
  14.  
  15. [Display(Name = "ID")]
  16. public string Id { get; set; }
  17.  
  18. [Display(Name = "帮助链接")]
  19. public string HelpLink { get; set; }
  20.  
  21. [Display(Name = "错误信息")]
  22. public string Message { get; set; }
  23.  
  24. [Display(Name = "来源")]
  25. public string Source { get; set; }
  26.  
  27. [Display(Name = "堆栈")]
  28. public string StackTrace { get; set; }
  29.  
  30. [Display(Name = "目标页")]
  31. public string TargetSite { get; set; }
  32.  
  33. [Display(Name = "程序集")]
  34. public string Data { get; set; }
  35.  
  36. [Display(Name = "发生时间")]
  37. public DateTime? CreateTime { get; set; }
  38. }
  39. }

SysExceptionModel.cs

创建SysException的BLL层和DAL层

  1. using System;
  2. using App.Models;
  3. using System.Linq;
  4. namespace App.IDAL
  5. {
  6. public interface ISysExceptionRepository
  7. {
  8. int Create(SysException entity);
  9. IQueryable<SysException> GetList(DBContainer db);
  10. SysException GetById(string id);
  11. }
  12. }

ISysExceptionRepository

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using App.Models;
  6. using App.IDAL;
  7.  
  8. namespace App.DAL
  9. {
  10. public class SysExceptionRepository:IDisposable, ISysExceptionRepository
  11. {
  12. /// <summary>
  13. /// 获取集合
  14. /// </summary>
  15. /// <param name="db">数据库</param>
  16. /// <returns>集合</returns>
  17. public IQueryable<SysException> GetList(DBContainer db)
  18. {
  19. IQueryable<SysException> list = db.SysException.AsQueryable();
  20. return list;
  21. }
  22. /// <summary>
  23. /// 创建一个对象
  24. /// </summary>
  25. /// <param name="db">数据库</param>
  26. /// <param name="entity">实体</param>
  27. public int Create( SysException entity)
  28. {
  29. using (DBContainer db = new DBContainer())
  30. {
  31. db.SysException.AddObject(entity);
  32. return db.SaveChanges();
  33. }
  34.  
  35. }
  36.  
  37. /// <summary>
  38. /// 根据ID获取一个实体
  39. /// </summary>
  40. /// <param name="id"></param>
  41. /// <returns></returns>
  42. public SysException GetById(string id)
  43. {
  44. using (DBContainer db = new DBContainer())
  45. {
  46. return db.SysException.SingleOrDefault(a => a.Id == id);
  47. }
  48. }
  49. public void Dispose()
  50. {
  51.  
  52. }
  53. }
  54. }

SysExceptionRepository

  1. using System;
  2. using System.Collections.Generic;
  3. using App.Common;
  4. using App.Models;
  5. namespace App.IBLL
  6. {
  7. public interface ISysExceptionBLL
  8. {
  9. List<SysException> GetList(ref GridPager pager,string queryStr);
  10. SysException GetById(string id);
  11. }
  12. }

ISysExceptionBLL

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Practices.Unity;
  6. using App.IDAL;
  7. using App.Common;
  8. using App.Models.Sys;
  9. using App.Models;
  10. using App.IBLL;
  11. namespace App.BLL
  12. {
  13. public class SysExceptionBLL: ISysExceptionBLL
  14. {
  15. [Dependency]
  16. public ISysExceptionRepository exceptionRepository { get; set; }
  17.  
  18. public List<SysException> GetList(ref GridPager pager, string queryStr)
  19. {
  20. DBContainer db = new DBContainer();
  21. List<SysException> query = null;
  22. IQueryable<SysException> list = exceptionRepository.GetList(db);
  23. if (!string.IsNullOrWhiteSpace(queryStr))
  24. {
  25. list = list.Where(a => a.Message.Contains(queryStr));
  26. pager.totalRows = list.Count();
  27. }
  28. else
  29. {
  30. pager.totalRows = list.Count();
  31. }
  32.  
  33. if (pager.order == "desc")
  34. {
  35. query = list.OrderByDescending(c => c.CreateTime).Skip((pager.page - ) * pager.rows).Take(pager.rows).ToList();
  36. }
  37. else
  38. {
  39. query = list.OrderBy(c => c.CreateTime).Skip((pager.page - ) * pager.rows).Take(pager.rows).ToList();
  40. }
  41.  
  42. return query;
  43. }
  44. public SysException GetById(string id)
  45. {
  46. return exceptionRepository.GetById(id);
  47. }
  48. }
  49. }

SysExceptionBLL

创建SysException的Controller

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. using System.Reflection;
  8. using System.Text;
  9. using App.Common;
  10. using App.Models;
  11. using App.IBLL;
  12. using App.Models.Sys;
  13. using Microsoft.Practices.Unity;
  14.  
  15. namespace App.Admin.Controllers
  16. {
  17. public class SysExceptionController : Controller
  18. {
  19. //
  20. // GET: /SysException/
  21. [Dependency]
  22. public ISysExceptionBLL exceptionBLL { get; set; }
  23.  
  24. public ActionResult Index()
  25. {
  26.  
  27. return View();
  28.  
  29. }
  30.  
  31. public JsonResult GetList(GridPager pager, string queryStr)
  32. {
  33. List<SysException> list = exceptionBLL.GetList(ref pager, queryStr);
  34. var json = new
  35. {
  36. total = pager.totalRows,
  37. rows = (from r in list
  38. select new SysException()
  39. {
  40. Id = r.Id,
  41. HelpLink =r.HelpLink,
  42. Message = r.Message,
  43. Source = r.Source,
  44. StackTrace = r.StackTrace,
  45. TargetSite = r.TargetSite,
  46. Data = r.Data,
  47. CreateTime = r.CreateTime
  48. }).ToArray()
  49.  
  50. };
  51. return Json(json);
  52. }
  53.  
  54. #region 详细
  55.  
  56. public ActionResult Details(string id)
  57. {
  58.  
  59. SysException entity = exceptionBLL.GetById(id);
  60. SysExceptionModel info = new SysExceptionModel()
  61. {
  62. Id = entity.Id,
  63. HelpLink = entity.HelpLink,
  64. Message = entity.Message,
  65. Source = entity.Source,
  66. StackTrace = entity.StackTrace,
  67. TargetSite = entity.TargetSite,
  68. Data = entity.Data,
  69. CreateTime = entity.CreateTime,
  70. };
  71. return View(info);
  72. }
  73.  
  74. #endregion
  75.  
  76. }
  77.  
  78. }

SysExceptionController

创建SysException的Index视图和Details视图

  1. @using App.Admin;
  2. @using App.Common;
  3. @using App.Models.Sys;
  4.  
  5. @{
  6. ViewBag.Title = "Index";
  7. Layout = "~/Views/Shared/_Index_Layout.cshtml";
  8.  
  9. }
  10. <script src="~/Scripts/jquery.easyui.plus.js"></script>
  11. <div class="mvctool">
  12. <input id="txtQuery" type="text" class="searchText"/>
  13. <a id="btnQuery" style="float: left;" class="l-btn l-btn-plain"><span class="l-btn-left"><span class="l-btn-text icon-search" style="padding-left: 20px;">查询</span></span></a><div class="datagrid-btn-separator"></div>
  14. <a id="btnDetails" style="float: left;" class="l-btn l-btn-plain"><span class="l-btn-left"><span class="l-btn-text icon-details" style="padding-left: 20px;">详细</span></span></a><div class="datagrid-btn-separator"></div>
  15. <a id="btnDelete" style="float: left;" class="l-btn l-btn-plain"><span class="l-btn-left"><span class="l-btn-text icon-remove" style="padding-left: 20px;">删除</span></span></a>
  16. </div>
  17.  
  18. <table id="List"></table>
  19. <div id="Pager"></div>
  20. <div id="modalwindow" class="easyui-window" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
  21. @*Jqgrid*@
  22. <script type="text/javascript">
  23.  
  24. $(function () {
  25. $('#List').datagrid({
  26. url: '/SysException/GetList',
  27. width: $(window).width() - ,
  28. methord: 'post',
  29. height: $(window).height() - ,
  30. fitColumns: true,
  31. sortName: 'Id',
  32. sortOrder: 'desc',
  33. idField: 'Id',
  34. pageSize: ,
  35. pageList: [, , , , ],
  36. pagination: true,
  37. striped: true, //奇偶行是否区分
  38. singleSelect: true,//单选模式
  39. columns: [[
  40. { field: 'Id', title: 'ID', width: , hidden: true },
  41. { field: 'HelpLink', title: '帮助链接', width: },
  42. { field: 'Message', title: '异常信息', width: },
  43. { field: 'Source', title: '来源', width: },
  44. { field: 'StackTrace', title: '堆栈', width: , align: 'center' },
  45. { field: 'TargetSite', title: '目标页', width: , align: 'center' },
  46. { field: 'Data', title: '程序集', width: , align: 'center' },
  47. { field: 'CreateTime', title: '发生时间', width: , align: 'center' }
  48. ]]
  49. });
  50. });
  51. </script>
  52.  
  53. @*operation*@
  54. <script type="text/javascript">
  55. //ifram 返回
  56. function frameReturnByClose() {
  57. $("#modalwindow").window('close');
  58. }
  59. $(function () {
  60. $("#btnDetails").click(function () {
  61. var row = $('#List').datagrid('getSelected');
  62. if (row != null) {
  63.  
  64. $("#modalwindow").html("<iframe width='100%' height='98%' frameborder='0' src='/SysException/Details?id=" + row.Id + "'></iframe>");
  65. $("#modalwindow").window({ title: '详细', width: , height: , iconCls: 'icon-details' }).window('open');
  66. } else { $.messageBox5s('提示', '请选择要操作的行!'); }
  67. });
  68. $("#btnQuery").click(function () {
  69. var queryStr = $("#txtQuery").val();
  70. //如果查询条件为空默认查询全部
  71. if (queryStr == null) {
  72. queryStr = "%";
  73. }
  74. $('#List').datagrid({ url: '/SysException/GetList?queryStr=' + encodeURI(queryStr) });
  75. });
  76.  
  77. });
  78. </script>

Index

  1. @model App.Models.Sys.SysExceptionModel
  2. @using App.Admin;
  3. @using App.Common;
  4.  
  5. @using App.Models.Sys;
  6. @{
  7. ViewBag.Title = "Details";
  8. Layout = "~/Views/Shared/_Index_LayoutEdit.cshtml";
  9. }
  10. <script type="text/javascript">
  11. $(function () {
  12.  
  13. $("#btnReturn").click(function () {
  14. window.parent.frameReturnByClose();
  15. });
  16. });
  17. </script>
  18. <div class="mvctool bgb">
  19. <a id="btnReturn" style="float: left;" class="l-btn l-btn-plain"><span class="l-btn-left"><span class="l-btn-text icon-return" style="padding-left: 20px;">返回</span></span></a>
  20. </div>
  21. @using (Html.BeginForm())
  22. {
  23. <div id="ErrMesList">
  24. <div id="ErrMesListContent">
  25. @Html.ValidationSummary(false)
  26. </div>
  27. </div>
  28. <table class="form_table setinput355">
  29. <tbody>
  30. <tr>
  31. <th>
  32. @Html.LabelFor(model => model.HelpLink)
  33. </th>
  34. <td>
  35. @Html.EditorFor(model => model.HelpLink)
  36. </td>
  37. </tr>
  38.  
  39. <tr>
  40. <th>
  41. @Html.LabelFor(model => model.Message)
  42. </th>
  43. <td>
  44. @Html.TextAreaFor(model => model.Message, new { @style = "height:100px;width:550px" })
  45. </td>
  46. </tr>
  47.  
  48. <tr>
  49. <th>
  50. @Html.LabelFor(model => model.Source)
  51. </th>
  52. <td>
  53. @Html.EditorFor(model => model.Source)
  54. </td>
  55. </tr>
  56.  
  57. <tr>
  58. <th>
  59. @Html.LabelFor(model => model.StackTrace)
  60. </th>
  61. <td>
  62.  
  63. @Html.TextAreaFor(model => model.StackTrace, new { @style = "height:100px;width:550px" })
  64. </td>
  65. </tr>
  66.  
  67. <tr>
  68. <th>
  69. @Html.LabelFor(model => model.TargetSite)
  70. </th>
  71. <td>
  72. @Html.TextAreaFor(model => model.TargetSite, new { @style = "height:100px;width:550px" })
  73. </td>
  74. </tr>
  75.  
  76. <tr>
  77. <th>
  78. @Html.LabelFor(model => model.Data)
  79. </th>
  80. <td>
  81. @Html.EditorFor(model => model.Data)
  82. </td>
  83. </tr>
  84.  
  85. <tr>
  86. <th>
  87. @Html.LabelFor(model => model.CreateTime)
  88. </th>
  89. <td>
  90. @Html.TextBoxFor(model => model.CreateTime)
  91. </td>
  92. </tr>
  93.  
  94. </tbody>
  95. </table>
  96. }

Details

被忘记注入到容器。预览一下

由于时间关系,把异常和日志的应用放到一下讲吧。

然后我认为无目的的提供源码对园友的帮助是不大的,只能说你拥有一套源码,无论多漂亮都好,你自己不思考不动手,东西永远还是别人做出来的,真正遇到问题,是难解决,或者解决不了的,然而系统,我并没有完完全全把所有代码放出来,但是复杂的逻辑或者重点我都放出来了,正如上面,日志和异常的删除功能我没有放出源代码,就希望大家一起来完善这个强大的系统。

我希望大家如果有时间跟着来做,你肯定会受益匪浅

构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(11)-系统日志和异常的处理①的更多相关文章

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)

    转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...

  2. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充 系列目录 补充一下,有人要表单的代码,这个用代码生成器生成表Flow_Form表 ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支 系列目录 步骤设置完毕之后,就要设置好流转了,比如财务申请大于50000元( ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤 系列目录 步骤设计很重要,特别是规则的选择. 我这里分为几个规则 1.按自行 ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单 系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计 系列目录 建立好42节的表之后,每个字段英文表示都是有意义的说明.先建立 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01 工作流在实际应用中还是比较广泛,网络中存在很多工作流的图形化插件,可以做到拉拽的工 ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-【过滤器+Cache】

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-[过滤器+Cache] 系列目录 上次的探讨没有任何结果,我浏览了大量的文章 ...

  10. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当 ...

随机推荐

  1. C# Web应用调试开启外部访问

    在用C#开发Web应用时有个痛点,就是本机用VS开启Web应用调试时外部机器无法访问此Web应用.这里将会介绍如何通过设置允许局域网和外网机器访问本机的Web应用. 目录 1. 设置内网访问 2. 设 ...

  2. 《Web 前端面试指南》1、JavaScript 闭包深入浅出

    闭包是什么? 闭包是内部函数可以访问外部函数的变量.它可以访问三个作用域:首先可以访问自己的作用域(也就是定义在大括号内的变量),它也能访问外部函数的变量,和它能访问全局变量. 内部函数不仅可以访问外 ...

  3. java head space/ java.lang.OutOfMemoryError: Java heap space内存溢出

    上一篇JMX/JConsole调试本地还可以在centos6.5 服务器上进行监控有个问题端口只开放22那么设置的9998端口 你怎么都连不上怎么监控?(如果大神知道还望指点,个人见解) 线上项目出现 ...

  4. Vue + Webpack + Vue-loader 系列教程(1)功能介绍篇

    原文地址:https://lvyongbo.gitbooks.io/vue-loader/content/ Vue-loader 是什么? vue-loader 是一个加载器,能把如下格式的 Vue ...

  5. 手动添加kdump

    背景:     Linux嵌入式设备内核挂死后,无法自动重启,需要手动重启.而且如果当时没有连串口的话,就无法记录内核挂死时的堆栈,所以需要添加一种方式来记录内核挂死信息方便以后调试使用.设备中增加k ...

  6. 使用mybatis-generator在自动生成Model类和Mapper文件

    使用mybatis-generator插件可以很轻松的实现mybatis的逆向工程,即,能通过表结构自动生成对应的java类及mapper文件,可以大大提高工作效率,并且它提供了很多自定义的设置可以应 ...

  7. .Net Core上用于代替System.Drawing的类库

    目前.Net Core上没有System.Drawing这个类库,想要在.Net Core上处理图片得另辟蹊径. 微软给出了将来取代System.Drawing的方案,偏向于使用一个单独的服务端进行各 ...

  8. Java消息队列--ActiveMq 实战

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  9. BPM配置故事之案例4-子表

    公司渐渐对采购管理重视起来了,新招聘了采购主管老李,老李对现有的申请表很不满意,要求将申请物资和申请原因改成物资明细表 物资明细表 小明只好继续致电大毛-- 大毛:把申请物资和申请原因删掉,新增一个数 ...

  10. PostGIS(解压版)安装

    1.软件下载 postgresql-9.6.1-1-windows-x64-binaries.zip https://www.postgresql.org/download/windows/ post ...