以往開發程式一定要處理log的問題,不然還真的不知道該怎麼去抓蟲。log有幾種常見的方式,寫檔案、寫到資料庫或是寫入事件檢視器中,其實在Visual Studio工具中,只要用NuGet就可以非常簡單的方式開始記錄log,以下文章是介紹最好用的四種Log套件,可以參考看看:
https://www.codeproject.com/Articles/1278018/Best-Logging-libraries-for-ASP-NET-MVC
筆者推薦兩套,原因如下:
- 推薦Elmah:只要網站發生任何錯誤導致有error,就會寫log(不用在程式碼裡面特別去寫要記錄log)。IIS的error頁面要localhost端才看得到,但又不見得可以重現,所以有這個之後會方便許多。
- 推薦NLog :紀錄log還有一種情況,例如登入登出的紀錄,這個就不適合用Elmah拉,小而巧的 NLog 與 Log4Net 就很適合,兩套都不錯且有大量粉絲,對Log都能進行分級(一般紀錄「Debug、Info、Warn」、嚴重紀錄「Error、Fatal」),也都能紀錄到資料庫中, 而推薦NLog 的原因,是因為用的人好像比較多一些。
Elmah安裝教學
- 用NuGet下載 Elmah.Mvc,下載完成後 https://localhost:44345/elmah 就可以看到重大錯誤的log紀錄了,但是因為是放在記憶體中,網站關掉就沒有了。
- 在用NuGet下載 elmah.sqlserver 。
- 修改Web.config 會發現多了一段 <add name=”elmah-sqlserver” ………> 把正確的SqlServer設定上去,或是砍掉這段用既有的,但是要修改<errorLog type=”Elmah.SqlErrorLog, Elmah”connectionStringName=”正確連線名稱”/>
- 執行專案下 ~\App_Readme\Elmah.SqlServer.sql 建立資料表,執行完成後,目錄 ~\App_Readme 可以刪除。
- 增加安全控管,修改Web.config檔案,通常在檔案最後。預設只能本機觀看,可以改成遠端,把<security allowRemoteAccess=”false”/>改成 <security allowRemoteAccess=”true”/> 。把 path=”elmah.axd” 改成path=”elmah”,這個一定要改,因為MVC架構原因,會自動省略副檔名。把原本註解的authorization改出來,如下:
<location path="elmah" inheritInChildApplications="false">
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="Test/elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<!--
See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for
more information on using ASP.NET authorization securing ELMAH.
-->
<authorization>
<allow roles="admin" />
<allow users="帳號1,帳號2" />
<deny users="*" />
</authorization>
</system.web>
<system.webServer>
<handlers>
<add name="ELMAH" verb="POST,GET,HEAD" path="elmah" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
</location>
Nlog安裝教學
- 用NuGet下載 Nlog.Confg,有相依性就裝一裝。
- 專案資料夾會多出兩個檔案 NLog.config 這個是主要設定檔案, NLog.xsd 這個是XML描述檔可以不用理他。
- 修改 NLog.config 因為是Web專案所以要把位置${basedir}/logs/ 改成 ${basedir}/App_Data/logs/,然後設定rules等級,如下,就可以開始Logging了。
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
-->
<target xsi:type="File" name="f" fileName="${basedir}/App_Data/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
-->
<logger name="*" minlevel="Debug" writeTo="f" />
</rules>
</nlog>
至於要怎麼寫log呢,參考以下C# Code:
Logger logger = LogManager.GetCurrentClassLogger();
logger.Trace("**** Trace *** ");
logger.Debug("**** Debug ***");
logger.Info("**** Info ***");
logger.Warn("**** Warn ***");
logger.Error("**** Error ***");
logger.Fatal("**** Fatal ***");
NLog也可以改成寫到資料庫中,不過資料庫如果掛了,怎麼寫log呢,所以寫檔案log不見得不好喔,那就下回在寫吧。