C
C#2w ago
Stefan

EventLogAppender and log4net

Hey there, I was looking into configuring Windows event logging for an ASP.NET Core application (running on net8.0) with log4net. In brief, my log4net.config configuration looks something like this:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="MyOrg.WebApi.Logging.NxPatternLayout">
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<!--<logName value="MyOrg" /> -->
<applicationName value="MyOrg.Backend" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="Console" />
<appender-ref ref="EventLogAppender" />
</root>
<logger name="EventLogger">
<level value="ERROR" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="MyOrg.WebApi.Logging.NxPatternLayout">
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<!--<logName value="MyOrg" /> -->
<applicationName value="MyOrg.Backend" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="Console" />
<appender-ref ref="EventLogAppender" />
</root>
<logger name="EventLogger">
<level value="ERROR" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
I am using DI, so somewhere in my startup code I hook up log4net with options.AddLog4Net("log4net.config");. This works fine with everything except the Windows event logger. I registered a new source via PowerShell prior to running the application with:
New-EventLog -Source MyOrg.Backend -LogName MyOrg
New-EventLog -Source MyOrg.Backend -LogName MyOrg
Also tried variants of this (-LogName Application, etc.) to no avail. An older SO answer suggest that this is not working for .NET applications anymore (so only .NET Framework and .NET Standard compatible versions), see also: https://stackoverflow.com/questions/61639853/c-sharp-windows-event-viewer I looked at the source code here to confirm that: https://github.com/apache/logging-log4net/blob/master/src/log4net/log4net.csproj#L22C23-L22C44 The project is targeting .NET Framework 4.62 and .NET Standard 2.0. So my takeaway is, that .NET (Core) does not support Windows Event Logs?
Stack Overflow
C# windows event viewer
I want to log the errors from my C# app in Windows Event Viewer using log4net with the EventLogAppender (the errors must be logged under the Application log) I have a log4net.config class with this...
GitHub
logging-log4net/src/log4net/log4net.csproj at master · apache/loggi...
Apache Log4net is a versatile, feature-rich, efficient logging API and backend for .NET - apache/logging-log4net
2 Replies
Stefan
StefanOP2w ago
Note: I realize that the Windows Event Log only is supported on Windows, but I was working under the impression that the library was smart enough to figure that out on its own? Ah, looking at this table: https://logging.apache.org/log4net/release/framework-support.html I can see that .NET 8 support is provided by .NET Standard 2.0, which means EventLogAppender is not supported unless I switch to .NET Frameowkr
wasabi
wasabi2w ago
If you're building a .NET 8 application, there is no reason at all to be using log4net, since it has a built in DI based logging framework.

Did you find this page helpful?