ELMAH+MVC4+SQLite 错误日志

  任何程序我想无论是在调试开发阶段还是上线运营阶段,都能够使人“放心”,不要出什么意外,也不要玩什么心跳;那就需要比较到位和及时的异常与错误日志模块。

本文将简要描述ELMAH、MVC4与SQLite这个中小程序的好伙伴般的精干小团队。

1. ELMAH 可以通过NuGet来获取,建议选择ELMAH.MVC这个版本主要针对MVC架构,基本不需要什么配置; 当然也可以选择原始的ELMAH版本,只不过是多了些配置而已。

 1
 2 <elmah>
 3 <!--  <errorMail from="roger_zhang@newsoft.com.cn"
 4              to="mqgh@sina.com"
 5              subject="Roger is testing ELMAH log Modeul"
 6              async="true"
 7              smtpPort="25"
 8              smtpServer="smtpcom.263xmail.com"
 9              userName="XXX"
10              password="*****" />-->
11   <!--<errorLog type="Elmah.MemoryErrorLog, Elmah" size="100" />-->
12   <!--<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/ElmahXML_Logs" />-->
13   <errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="ElmahDB" />
14 </elmah>

可以通过以上配置来分别测试三种不同的日志记录模式:MemoryErrorLog、XmlFileErrorLog、SQLiteErrorLog、Email。

2. Controller端简单的写两个异常测试一下,看看效果,混个脸熟先。

 1  public ActionResult Index()
 2         {
 3             ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。";                  
 4             return View();
 5         }
 6  public PartialViewResult About()
 7         {
 8             ViewBag.Message = "Your app description page.";
 9             var a = _db.CardRecordItem.SingleOrDefault(x => x.CardId == 999999999).CardId;//此处会服空引用异常,因为本表中不存在这个ID
10             return PartialView();
11         }
12  public ActionResult Contact()
13         {
14             ViewBag.Message = "Your contact page.";
15             int num = Convert.ToInt32("XXX");//此处会报类型转换异常
16             return View();
17         }

3.SQLite的配合。same step 用NuGet来获取最新版的SQLite,并在web.config文件中做相应的配置。

  <add name="ElmahDB" connectionString="data source=~/App_Data/Elmah.db" />

4.测试结果出来啦,只需要点About和Contact两个页面即可。

5.补充sqlite的简单操作,来确认此Error数据是否存入相应数据库。

下载Sqlite.exe假如放在D:\Programefiles\处; 相应的Elmah.db放在App_Data下。

D:\Program Files>sqlite3.exe H:\GitHub\RogerHelloWorld\MvcApplication14\App_Dat\Elmah.db;
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

接下来就可以Selet喽。

 

posted @ 2013-10-17 18:20  政政糖  阅读(8414)  评论(0编辑  收藏  举报