No EventLogAppender in Log4Net 2.0.14 - log4net

I'm trying to configure an EventLogAppender in a .Net 6.0 console app, I'm using Log4Net 2.0.14 and I getting the following Exception...
log4net:ERROR Could not create Appender [EventLogAppender] of type [log4net.Appender.EventLogAppender]. Reported error follows.
System.TypeLoadException: Could not load type [log4net.Appender.EventLogAppender]. Tried assembly [log4net, Version=2.0.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a] and all loaded assemblies
at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR Appender named [EventLogAppender] not found.
This is my appender:
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Application" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ERROR"/>
<appender-ref ref="EventLogAppender"/>
</root>
</log4net>

See the official EventLogAppender doc:
Please note that this appender is not natively available in log4net for dotnet standard, as it would bring in windows dependencies for non-windows consumers. Please see https://www.nuget.org/packages/log4net.appenders.netcore for an alternative if you still require EventLog logging for netstandard targets
So use the Nuget package "Log4Net.Appenders.NetCore".
The appender declaration is slightly different:
<appender name="EventLogAppender" type="Log4Net.Appenders.NetCore.EventLogAppender,Log4Net.Appenders.NetCore">

Related

Configure Log4Net to enable me to debug issues with a Log4Net Splunk appender?

Can someone clue me in to how I might configure Log4net to help with more verbose debugging?
Currently I have a project where I would like to use a Log4Net appender to log to Splunk.
There exists a NuGet package that seems popular: https://github.com/AlanBarber/log4net.Appender.Splunk
Only there is some problem with the URL and I can't figure out how configure either Log4Net or the appender to help me understand what is wrong with my config.
Here is my config:
<configuration>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<threshold value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="Console Logger: %date %-5level: %message%newline" />
</layout>
</appender>
<appender name="Splunk" type="log4net.Appender.Splunk.SplunkHttpEventCollector, log4net.Appender.Splunk">
<ServerUrl>https://my_trial_cloud_splunk_server_name.cloud.splunk.com:8088/services/collector</ServerUrl>
<Token>de63d82e-6f5d-4f72-a03c-018d462c30c8</Token>
<RetriesOnError>10</RetriesOnError>
<BatchIntevalMs>100</BatchIntevalMs>
<BatchSizeCount>10</BatchSizeCount>
<SendMode>Parallel</SendMode>
<IgnoreCertificateErrors>True</IgnoreCertificateErrors>
<threshold value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level [%thread]: %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="Console" />
<appender-ref ref="Splunk" />
</root>
</log4net>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
I've set the log4net debug to true. Only, even if I have the wrong URL I don't see any log information saying there was trouble with the Splunk endpoint. How do I get more verbose debugging that there is some issue?

How to remove the word "FATAL" in log.fatal() in log4net. Example [2018-10-26 09:37:14,889] FATAL Log Message

How to remove the word "FATAL" in log.fatal() in log4net. Example [2018-10-26 09:37:14,889] FATAL Log Message. How to remove FATAL from this by using log4net configuration? or its a default in log.fatal()?
This can be done by editing your configuration and removing [%level] field from the layout pattern.
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<threshold value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date]-[%thread]-**[%level]**-[%logger] - %message%newline" />
</layout>
</appender>

override log4net appender threshold

I would like to know if it's possible to override a log4net appender threshold.
Default threshold level for the appender is ERROR, but for some namespaces i would like to log INFO as well, e.g. program start/stop info messages.
For example override the log level like this.
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<threshold value="ERROR"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
<logger name="My.Main.Program">
<level value="INFO" />
</logger>
</appender>
Thanks in advance.

Log4Net does not log from a class library

I am working on .NET Framework 4.0 using C# in Windows 7, and trying to log from a class library but it's not working. I'm running my application without errors, but also nothing happens to my log file, neither to my console.
So, here is my code:
This is my App.config file:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<add key="log4net.config" value="config.log4net"/>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MyLogFile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="debug"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<logger name="MyApplication">
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
This is what I put into my AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
This what is at the file i'm trying to log:
private static readonly ILog log = LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Debug("Testing");
When i run my program nothing happens. Someone knows why?
Similar to this answer, both the app.config, log4net configuration and the AssemblyInfo.cs configurator needs to be placed in the host application.
Lets say I have project Console as a console project that I will run, and Library as a Library project. Console will need to have an app.config file with the above log4net configuration, and the AssemblyInfo.cs will need the XmlConfigurator code inside of it. Library does not need either of these, and will work by using the LogManager call.
Console > This is the project that you will run.
Properties
AssemblyInfo.cs > Place [assembly: log4net.Config.XmlConfigurator(Watch = true)] here.
app.config > Place log4net XML configuration here.
Program.cs > Start of application.
Library
Properties
AssemblyInfo.cs > Leave this file alone.
Foo.cs > Create the private readonly property.
SomeMethod() > log.Debug("Test");
Incase anyone is still looking at this and to add to the previous answers, you need to have instantiated a logger in your host application before you can log from the class library.
If you look at the log4net documentation for assembly attributes it says this:
"Therefore if you use configuration attributes you must invoke log4net
to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly
to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and
certainly before any external assemblies have been loaded and invoked."
When the assembly attributes are defined in a class library - i.e. an external assembly - it gets tricky. Can you use log4net.Config.XmlConfigurator.Configure(path) instead?

Log events from another assembly in a seperate log4net log

I have the following log4net configuration
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\\CurrentLog.txt"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
</layout>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
Is it possible to log the events from a different assembly in a different file? For example, messages from
Castle.Facilities.NhibernateIntegration
If not that, is there a better way of filtering the events?
I do not want to limit the stream to only errors.
Loggers can be configured by their names. If you're following idiomatic log4net practices, your loggers are named by their full Namespace.TypeName. You can assign appenders to loggers in two ways.
The first is to embed the special appender directly
<logger name="Castle.Facilities.NhibernateIntegration">
<level value="INFO" />
<appender name="CastleNhIntegrationAppender" type="...">
<!-- put the full appender configuration here -->
</appender>
</logger>
Or to define the appender and reference it in the logger
<appender name="CastleNhIntegrationAppender" type="...">
<!-- put the full appender configuration here -->
</appender>
<logger name="Castle.Facilities.NhibernateIntegration">
<level value="INFO" />
<appender-ref ref="CastleNhIntegrationAppender" />
</logger>

Resources