gpt4 book ai didi

c# - 如何使用 .net core 的 ILoggerFactory 写入自定义事件源?有没有办法指定来源?

转载 作者:行者123 更新时间:2023-12-04 01:15:29 26 4
gpt4 key购买 nike

我正在尝试写入 .netcore/c# 中的自定义事件源,但还没有找到指定 .net core 记录器对象的目标源的方法。在这种情况下,我想写入“我的事件日志”而不是应用程序日志。下面的代码成功写入应用程序日志,但我想将其指向“我的事件日志”事件源。

if (!EventLog.SourceExists("My Event Log"))
{
EventLog.CreateEventSource("My Event Log", "My Program");
}

ILoggerFactory loggerFactory = new LoggerFactory()
.AddConsole()
.AddDebug()
.AddEventLog();

ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("DAILY LOAD starting...");

最佳答案

下面的代码和解释使我的 .NETCore/C# 控制台应用程序成功写入事件查看器中的“自定义应用程序日志”没有事件 ID 描述错误 (如果您遇到此问题,请参阅 问题 #1 修复 )。

//libraries needed..
//using Microsoft.Extensions.Logging;
//using Microsoft.Extensions.Logging.EventLog;

string _sourceName = "My Program"; //source program name
string _logName = "My Event Log"; //new event log or targeted event log name

//if custom event log does not exist, create it
if (!EventLog.SourceExists(_logName))
{
//event log creates new app log and associates program, "My Program", with new log, "My Event Log"
EventLog.CreateEventSource(_sourceName, _logName);
}

EventLogSettings myEventLogSettings = new EventLogSettings
{
SourceName = _sourceName,
LogName = _logName
};

ILoggerFactory loggerFactory = new LoggerFactory()
.AddConsole()
.AddDebug()
.AddEventLog(myEventLogSettings);

ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation(1000, "DAILY LOAD starting...");

注意 #1 :如果您要更改现有源程序的目标日志...或者换句话说,如果您要重新指向一个已经写入事件日志的程序,这将需要重新启动才能注册。如果日志是新创建的,则写入成功。有关更多信息,请参阅此线程: Windows Event Log - how to register an event source?

问题 #1 :当您打开事件查看器查看自定义应用程序日志的第一个事件时, 你会看到你的事件被埋在 error message .

找不到源“我的程序”中事件 ID 0 的描述。您的本地计算机上未安装引发此事件的组件或安装已损坏。您可以在本地计算机上安装或修复该组件。
如果事件起源于另一台计算机,则显示信息必须与事件一起保存。以下信息包含在事件中:
我的程序
DAILY LOAD 开始...消息资源存在但在字符串/消息表中找不到该消息

ISSUE #1 FIX: the new application log is either referencing a non-existing Message Table file or the EventId being passed is NOT in the Message Table. The easiest way to resolve this issue is to have your log reference an existing Message Table file (for example, .NET Runtime) and pass it a documented EventId (I used 1000). This requires a registry change, steps below (article for reference and screenshots):

  1. Open registry
  2. Navigate to Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog
  3. Click on the folder of the new event log. In this case, "My Event Log".
  4. Create new string in the folder.

    Value name = "EventMessageFile"
    Value data = "C:\Windows\System32\mscoree.dll"

IMPORTANT: the Value data path above is referencing the existing .NET Runtime Message table, you can reference a different message table if you would like. Because the .NET Runtime message table does NOT have an EventID equal to 0, I continued to get the same error. Once I changed the eventId being passed to an existing value in the .NET Runtime Message table (in this case, 1000), it worked as expected with NO error message!

关于c# - 如何使用 .net core 的 ILoggerFactory 写入自定义事件源?有没有办法指定来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54244285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com