你還讓系統瞇著眼睛上線嗎?其實,為系統增加 Log 機制是簡單的事。
這裡不討論是該採行資料庫或是文字檔記錄,或是系統錯誤時自動寄發 Email 通知,只單純地說明 Kuick Application Framework 裡提供的 Log 機制為何,想更廣泛了解目前開發 .Net 程式常使用的 log 機制,請參考以下連結。
What is your .NET logging framework of choice?
Kuick 內建的執行期記錄
程式實作:Kuick.Logger.cs
儲存格式:記錄成 .log 文字檔。
存放目錄 (log 資料夾可透過設定改變,這部份將留待後續說明):
檔案名稱:
Log 檔案切檔時機:
檔案切檔原則有 2 種設定,第一種是依據檔案大小切分 (RefreshSize,以數字設定,大小單位為 KB),另一種是依據時間切分 (RefreshFrequency,Annual 每年, Monthly 每月, Daily 每日, Hourly 每小時, Minutely 每分鐘),設定方式是在 Web.config / App.config 裡設定如下,如果兩種同時設定,會優先依據檔案大小切分。
<configuration>
<Kuick>
<kernel>
<!-- log 檔切檔大小(KB) -->
<add group="Log" name="RefreshSize" value="1024" />
<!-- log 檔切檔時間(Annual, Monthly, Daily, Hourly, Minutely) -->
<add group="Log" name="RefreshFrequency" value="Hourly" />
</kernel>
</Kuick>
</configuration>
Log 寫出等級 (Kuick.LogLevel):
使用 Logger:
記錄一般訊息
Logger.Message(
"Emailer.SendCompleted",
"email send",
new Any("Subject", subject)
);
記錄 Exception 錯誤
try {
} catch(Exception ex) {
Logger.Error("Title ...", ex);
}
IntervalLogger 透過實作 IDisposable 介面,產生區段 Log 並註記執行時間
using(IntervalLogger il = new IntervalLogger("Heartbeat.Start")) {
il.Add("ApplicationID", Current.AppID);
// ...
}
2012-10-13 09:02:44.844 43 >>> 3E9DA63535C441598B646E5CEC6517B0 Message
Title : Heartbeat.Start
1. Elapsed (in seconds) = 2.108
2. ApplicationID = KuickWebSite
其他完整使用方法,請直接參考 Kuick.Logger。
你對於 Log 功能的要求是什麼?歡迎回應討論。
另外提供系統初始化時,進行資料庫規格同步的追踨記錄
2012-10-13 09:02:44.604 36 >>> E1793925B7004E0790C420C059C7D67D Track
Title : Kuick.Data.DataStart.DoDatabaseStart
01. Elapsed (in seconds) = 1.238
02. Start: Entity Name = ConfigEntity
03. Database Version = Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
Jun 17 2011 00:54:03
Copyright (c) Microsoft Corporation
Express Edition with Advanced Services (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
04. Database Tables = T_CONFIG,T_MAPPING
05. Sync Approach = Table already exists: 1. drop indexes --> 2. sync columns --> 3. create indexes
06. Connection SessionID = 9541375CFCA44CE28079DC412033E5E1
07. SQL Command String = DROP INDEX [T_CONFIG].[T_CONFIG_Index_1]
08. Drop Index 'T_CONFIG_Index_1' = Success
09. Connection SessionID = 9541375CFCA44CE28079DC412033E5E1
10. SQL Command String = DROP INDEX [T_CONFIG].[T_CONFIG_Index_2]
11. Drop Index 'T_CONFIG_Index_2' = Success
12. Connection SessionID = 226BA2E0AA014844A554631C3DBD0907
13. SQL Command String = ALTER TABLE [T_CONFIG] ALTER COLUMN [FLAG] BIT NULL
14. Alter Column 'FLAG' = Success
15. Connection SessionID = 0CC19908657D42BAA254CE5DC2A93AB7
16. SQL Command String = CREATE UNIQUE INDEX T_CONFIG_Index_1 ON [T_CONFIG](
[APP_ID]
,[CATEGORY]
,[Path]
,[NAME]
)
17. Create Index 'T_CONFIG_Index_1' = Success
18. Connection SessionID = 0CC19908657D42BAA254CE5DC2A93AB7
19. SQL Command String = CREATE INDEX T_CONFIG_Index_2 ON [T_CONFIG](
[FLAG]
,[CREATE_DATE]
)
20. Create Index 'T_CONFIG_Index_2' = Success
21. End: Entity Name = ConfigEntity
22. Start: Entity Name = MappingEntity
23. Database Version = Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
Jun 17 2011 00:54:03
Copyright (c) Microsoft Corporation
Express Edition with Advanced Services (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
24. Database Tables = T_CONFIG,T_MAPPING
25. Sync Approach = Table already exists: 1. drop indexes --> 2. sync columns --> 3. create indexes
26. Connection SessionID = 29FBD877C765498AA9EEF4E855B72732
27. SQL Command String = DROP INDEX [T_MAPPING].[T_MAPPING_Index_1]
28. Drop Index 'T_MAPPING_Index_1' = Success
29. Connection SessionID = 29FBD877C765498AA9EEF4E855B72732
30. SQL Command String = DROP INDEX [T_MAPPING].[T_MAPPING_Index_2]
31. Drop Index 'T_MAPPING_Index_2' = Success
32. Connection SessionID = 442CA0D377D84F7F9775A87B0B4395D7
33. SQL Command String = ALTER TABLE [T_MAPPING] ALTER COLUMN [FLAG] BIT NULL
34. Alter Column 'FLAG' = Success
35. Connection SessionID = 5BF9F27E307A4BA8B1C9A2EE9D2F933B
36. SQL Command String = CREATE UNIQUE INDEX T_MAPPING_Index_1 ON [T_MAPPING](
[PRE_ORDER_ENTITY_NAME]
,[POST_ORDER_ENTITY_NAME]
,[PRE_ORDER_ID]
,[POST_ORDER_ID]
)
37. Create Index 'T_MAPPING_Index_1' = Success
38. Connection SessionID = 5BF9F27E307A4BA8B1C9A2EE9D2F933B
39. SQL Command String = CREATE INDEX T_MAPPING_Index_2 ON [T_MAPPING](
[FLAG]
,[CREATE_DATE]
)
40. Create Index 'T_MAPPING_Index_2' = Success
41. End: Entity Name = MappingEntity
========================================
鐵人賽分享列表:Kuick Application & ORM Framework
開放原始碼專案:kuick.codeplex.com
直接下載原始碼:Kuick
下載相關文件檔:C# Code Conventions and Design Guideline
相關教學影片區:Kuick on YouTube