什么网站可以有人做详情页,wordpress修改头部显示,百度账号登录个人中心,中国核工业二三建设有限公司官网每当出现一些未捕获异常时#xff0c;操作系统都会将异常信息写入到 Windows 事件日志 中#xff0c;可以通过 Windows 事件查看器 查看#xff0c;如下图#xff1a; 这篇文章将会讨论如何使用编程的方式将日志记录到 Windows 事件日志 中。 安装 EventLog 要想在 .NET Co…每当出现一些未捕获异常时操作系统都会将异常信息写入到 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.AddSingletonILogManager, NLogManager();当你打开 Windows 事件查看器就会看到错误信息已成功记录到这里了参考如下截图 Windows事件日志 通常用于记录 系统事件网络流量和诸如安全性能相关的信息 等等你也可以将应用程序的日志记录到 Windows事件日志中通常来说如果你的程序仅仅是跑在 windows 上那么将应用程序信息记录到 Windows事件日志 中是一个非常不错的选择。 译文链接https://www.infoworld.com/article/3598750/how-to-log-data-to-the-windows-event-log-in-csharp.html