I am trying to write to a custom event source in .netcore/c# but have not found a way to specify the target source of .net core's logger object. In this case, I want to write to "My Event Log" as opposed to the Application log. The code below writes successfully to the Application log, but I want to point it at the "My Event Log" event source.
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...");
Code and explanation below that got my .NETCore/C# console app writing to a "custom application log" in event viewer successfully WITHOUT Event ID Description errors (see ISSUE #1 FIX if you are having this issue).
NOTE #1: if you are changing the targeted log of an existing source program... or in other words, if you are re-pointing a program that is ALREADY writing to an event log, this will require a reboot to register. If the log is newly created, it will write successfully. See this thread for more info: Windows Event Log - how to register an event source?
ISSUE #1: When you open event viewer to take a look at your custom application log's first event, you will see that your event is buried in an error message.
The description for Event ID 0 from source My Program cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer. If the event originated on another computer, the display information had to be saved with the event.The following information was included with the event: myProgram DAILY LOAD starting...the message resource is present but the message is not found in the string/message table