<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何將日志記錄到 Windows事件日志 中

          共 4083字,需瀏覽 9分鐘

           ·

          2021-01-11 21:59

          每當(dāng)出現(xiàn)一些未捕獲異常時(shí),操作系統(tǒng)都會將異常信息寫入到 Windows 事件日志 中,可以通過 Windows 事件查看器 查看,如下圖:

          這篇文章將會討論如何使用編程的方式將日志記錄到 Windows 事件日志 中。

          安裝 EventLog

          要想在 .NET Core 中記錄數(shù)據(jù)到 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 清空日志

          為了能夠?qū)崿F(xiàn)清空所有 windows 日志,可以使用如下代碼:


          EventLog?eventLog?=?new?EventLog();
          eventLog.Source?=?"MyEventLogSource";
          eventLog.Clear();

          Clear 是清空所有的 windows 事件日志,那如何清除某一個(gè)類別的日志呢?比如說: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 ,這個(gè)包封裝了連接 EventLog 錯(cuò)綜復(fù)雜的細(xì)節(jié),所以你只需要像平時(shí)用 NLog 一樣的操作即可。

          創(chuàng)建 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);
          ????}

          創(chuàng)建 NLogManager 類

          接下來,從 ILogManager 接口上派生一個(gè) 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 方法

          為了簡單起見,我就僅實(shí)現(xiàn) LogError 方法,其他的三個(gè)方法大家可以自行實(shí)現(xiàn),為了能夠了解如何通過 NLog 記錄日志到 Windows事件日志 中,修改代碼如下:


          ????public?void?LogError(string?message)
          ????{
          ????????Logger?logger?=?LogManager.GetLogger("EventLogTarget");
          ????????var?logEventInfo?=?new?LogEventInfo(LogLevel.Error,logger.Name,?message);
          ????????logger.Log(logEventInfo);
          ????}

          請注意,上面我創(chuàng)建了一個(gè)名為 EventLogTarget 的 EventLog,然后在 LogEventInfo 的構(gòu)造函數(shù)中傳遞 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 的完整代碼實(shí)例,可供大家參考。


          ????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 方法中進(jìn)行注入,代碼如下:


          services.AddSingleton();

          當(dāng)你打開 Windows 事件查看器,就會看到錯(cuò)誤信息已成功記錄到這里了,參考如下截圖:

          Windows事件日志 通常用于記錄 系統(tǒng)事件,網(wǎng)絡(luò)流量和諸如安全,性能相關(guān)的信息 等等,你也可以將應(yīng)用程序的日志記錄到 Windows事件日志中,通常來說,如果你的程序僅僅是跑在 windows 上,那么將應(yīng)用程序信息記錄到 Windows事件日志 ?中是一個(gè)非常不錯(cuò)的選擇。

          譯文鏈接:https://www.infoworld.com/article/3598750/how-to-log-data-to-the-windows-event-log-in-csharp.html


          瀏覽 37
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品福利在线观看 | FC2清純18歲在线播放 | 一区二区三区四区高清无码 | 九九九九九色 | 四虎5151精品成人无码 |