每當出現一些未捕獲異常時,作業系統都會將異常資訊寫入到 Windows 事件日誌
中,可以透過 Windows 事件檢視器
檢視,如下圖:
要想在 .NET Core 中記錄資料到 Windows 事件日誌中,可以用 Nuget 安裝一下 Microsoft.Extensions.Logging.EventLog
包,用 Visual Studio 中的 NuGet Package Manager
視覺化面板 或者 使用 NuGet Package Manager Console
命令列介面都可以,輸入命令如下:
Install-Package Microsoft.Extensions.Logging.EventLog
透過 EventLog 記錄日誌
要想將日誌寫入 Windows 事件日誌中,可以使用如下程式碼:
EventLog eventLog = new EventLog();eventLog.Source = "MyEventLogTarget";eventLog.WriteEntry("This is a test message.", EventLogEntryType.Information);
透過 EventLog 清空日誌
為了能夠實現清空所有 windows 日誌,可以使用如下程式碼:
EventLog eventLog = new EventLog();eventLog.Source = "MyEventLogSource";eventLog.Clear();
Clear 是清空所有的 windows 事件日誌,那如何清除某一個類別的日誌呢? 比如說:MyEventLogTarget,修改程式碼如下:
if (EventLog.Exists("MyEventLogTarget")){ EventLog.Delete("MyEventLogTarget");}
讀取 Windows 事件日誌 記錄可以使用 foreach 迭代 Entries 來獲取所有的日誌記錄。
EventLog eventLog = new EventLog();eventLog.Log = "MyEventLogTarget";foreach (EventLogEntry entry in eventLog.Entries){ //Write your custom code here}
使用 NLog 將日誌記錄到 Windows 事件日誌 中
要想使用 NLog 將日誌記錄到 windows事件日誌 中,你需要用 NuGet 安裝一下 NLog.WindowsEventLog
,這個包封裝了連線 EventLog 錯綜複雜的細節,所以你只需要像平時用 NLog 一樣的操作即可。
下面的介面方法用於記錄不同級別的日誌 (information, warning, debug, or error)
public interface ILogManager { void LogInformation(string message); void LogWarning(string message); void LogDebug(string message); void LogError(string message); }
建立 NLogManager 類接下來,從 ILogManager 介面上派生一個 NLogManager 類,程式碼如下:
public class NLogManager : ILogManager { private static NLog.ILogger logger = LogManager.GetCurrentClassLogger(); public void LogDebug(string message) { throw new NotImplementedException(); } public void LogError(string message) { logger.Error(message); } public void LogInformation(string message) { throw new NotImplementedException(); } public void LogWarning(string message) { throw new NotImplementedException(); } }
使用 LogError 方法
為了簡單起見,我就僅實現 LogError 方法,其他的三個方法大家可以自行實現,為了能夠了解如何透過 NLog 記錄日誌到 Windows事件日誌 中,修改程式碼如下:
public void LogError(string message) { Logger logger = LogManager.GetLogger("EventLogTarget"); var logEventInfo = new LogEventInfo(LogLevel.Error,logger.Name, message); logger.Log(logEventInfo); }
請注意,上面我建立了一個名為 EventLogTarget
的 EventLog,然後在 LogEventInfo 的建構函式中傳遞 log級別,logger的名字 以及 需要記錄的 log 資訊。
為了能夠配置 Nlog 以程式設計的方式 透過 EventLog 記錄日誌,可以使用如下程式碼。
var config = new NLog.Config.LoggingConfiguration();var logEventLog = new NLog.Targets.EventLogTarget("EventLogTarget");config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Error, logEventLog);NLog.LogManager.Configuration = config;
完整的 NLogManager 例子以下是 NLogManager 的完整程式碼例項,可供大家參考。
public class NLogManager : ILogManager { private static NLog.ILogger logger =LogManager.GetCurrentClassLogger(); public void LogDebug(string message) { logger.Debug(message); } public void LogError(string message) { Logger logger = LogManager.GetLogger("EventLogTarget"); var logEventInfo = new LogEventInfo(LogLevel.Error,logger.Name, message); logger.Log(logEventInfo); } public void LogInformation(string message) { logger.Info(message); } public void LogWarning(string message) { logger.Warn(message); } }
為了能夠在 Controller 中使用 NLogManager,還需要在 Startup 下的 ConfigureServices 方法中進行注入,程式碼如下:
services.AddSingleton<ILogManager, NLogManager>();
當你開啟 Windows 事件檢視器,就會看到錯誤資訊已成功記錄到這裡了,參考如下截圖:
Windows事件日誌 通常用於記錄 系統事件,網路流量和諸如安全,效能相關的資訊 等等,你也可以將應用程式的日誌記錄到 Windows事件日誌中,通常來說,如果你的程式僅僅是跑在 windows 上,那麼將應用程式資訊記錄到 Windows事件日誌 中是一個非常不錯的選擇。