在 Log 中可以看到像 NLog 的 caller 內容
NLog 記錄時可以看到 caller 是在什麼函式的程式碼行數,如果想轉移直接使用 MS Logger,這個功能常常會讓人無法放棄。
如果想在 MS Logger 上增加這些資訊,有兩個方向需要突破,
首先,需要了解怎麼找到記錄的程式碼位置,
再來,是如何擴充 MS Logger 來進行動作。
從 NLog 的原始碼來看,NLog 是靠著 CallSiteInformation 這個類別來取得程式碼位置,裡面是使用 .Net 的 StackTrace 類別來取得呼叫堆疊,
過濾掉從呼叫函式到 Log 方法間,像擴充方法之類的堆疊後,就能取得正確的程式碼位置。

為了完成這個目的,需要將 NLog 中 CallSiteInformation、以及這個類別所使用的公用函式庫 StackTraceUsageUtils 複製出來改寫。
接下來,需要來介入 MS Logger 的行為,需要在 Log State 中插入取得的 StackTrace 內容。
從原始碼來看,可以知道 MS Logger,會使用 LoggerFactory 來建立我們程式中使用的 Logger 物件,
所以,利用 Scurtor 裝飾 LoggerFactory 的話,就有機會介入 Log 行為。
我實作了 WarpLoggerFactory、WrapLogger,來進行介入。


接下來,整合改寫出來的 CallSiteInformation

這樣就完成了介入行為。
因為過濾不想記錄的 StackTrace 需要連同這個擴充的相關的部分一起過濾,所以把這些程式放在一個類別專案中,
接下來撰寫 DI 註冊的程式碼。

最後的專案結構

撰寫測試程式

最後得到結果

成功記錄了 caller。
程式碼放置在 GitHub:phoenix-chen-2016/AddStackTraceToMsLogger