C#C
C#12mo 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>


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


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
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
Apache Log4net is a versatile, feature-rich, efficient logging API and backend for .NET - apache/logging-log4net
Was this page helpful?