windows log日志分割_如何将日志记录到 Windows事件日志 中
每當出現一些未捕獲異常時,操作系統都會將異常信息寫入到 Windows 事件日志 中,可以通過 Windows 事件查看器 查看,如下圖:
這篇文章將會討論如何使用編程的方式將日志記錄到 Windows 事件日志 中。
安裝 EventLog
要想在 .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 一樣的操作即可。
創建 ILogManager 接口
下面的接口方法用于記錄不同級別的日志 (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 將日志記錄到 Windows事件日志 中
為了能夠配置 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事件日志 中是一個非常不錯的選擇。
譯文鏈接:https://www.infoworld.com/article/3598750/how-to-log-data-to-the-windows-event-log-in-csharp.html
總結
以上是生活随笔為你收集整理的windows log日志分割_如何将日志记录到 Windows事件日志 中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA求集合中的组合
- 下一篇: 秋叶一键重装系统连接服务器失败,秋叶一键