Log4net does not register with Windows Event Viewer

I want to log into the Windows Event Viewer using log4net.
I created a console application (.NET Framework 4), I added the link log4net.dll, I put the following code in my App.config:

<configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <log4net> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/> </layout> </appender> <root> <level value="ALL"/> <appender-ref ref="EventLogAppender"/> </root> </log4net> <startup><supportedRuntime version="v2.0.50727"/></startup> </configuration> 

And I put the following code:

 class Program { static void Main(string[] args) { log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); log.Error("test error", new Exception("error exception", new Exception("error innerexception"))); Console.Read(); } } 

This is not a magazine, nothing happens, why?

thanks

+6
source share
3 answers

You need to call configure .

Edit:

 [assembly: log4net.Config.XmlConfigurator(ConfigFile = "App.config", Watch = true)] 

To

 [assembly: log4net.Config.XmlConfigurator(Watch = true)] 

When you specify ConfigFile = "App.config" , it will search for App.config, but your name will be [FileName].Config .

+10
source

You need to call XmlConfigurator.Configure from the log4net library to initialize it. (see below)

 class Program { static void Main(string[] args) { // you need this XmlConfigurator.Configure(); log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); log.Error("test error", new Exception("error exception", new Exception("error innerexception"))); Console.Read(); } } 
+5
source

Call XmlConfigurator.Configure () at the beginning of your application.

You also need to grant the user the right to use applications in the event log.

Good way to do this with powershell, admin mode

  New-EventLog EventLogName -source ApplicationName 

Also add two parameters to appender

  <param name="LogName" value="EventLogName " /> <param name="ApplicationName" value="ApplicationName" /> 

Hi,

0
source

Source: https://habr.com/ru/post/891652/


All Articles