windows log日志分割_如何将日志记录到 Windows事件日志 中

本文详细介绍了如何在.NET Core中利用EventLog和NLog将日志记录到Windows事件日志中,包括安装EventLog包、记录、清空日志、读取日志记录、创建ILogManager接口、配置NLog以及如何在Controller中注入NLogManager。
摘要由CSDN通过智能技术生成

每当出现一些未捕获异常时,操作系统都会将异常信息写入到 Windows 事件日志 中,可以通过 Windows 事件查看器 查看,如下图:

255d993b199d8520764daae61513eca0.png

这篇文章将会讨论如何使用编程的方式将日志记录到 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);

7b323f8ac7fc5250a738b3b8a0c1e49e.png

通过 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 事件查看器,就会看到错误信息已成功记录到这里了,参考如下截图:

32f33e1a402f502c3a412589d143eb28.png

Windows事件日志 通常用于记录 系统事件,网络流量和诸如安全,性能相关的信息 等等,你也可以将应用程序的日志记录到 Windows事件日志中,通常来说,如果你的程序仅仅是跑在 windows 上,那么将应用程序信息记录到 Windows事件日志 中是一个非常不错的选择。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值