Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is Log4Net configured?

Given that you have

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

in your AssemblyInfo.cs file, when is Log4Net configured? Is it at the application start or when you use a logger for the first time?

like image 903
mgamer Avatar asked May 05 '11 12:05

mgamer


People also ask

How do I read the log4net configuration?

Typically the log4net configuration is specified using a file. This file can be read in one of two ways: The System.Configuration API is only available if the configuration data is in the application's config file; the file named MyApp.exe.config or Web.config.

What configurations are available for the Appenders in log4net?

These configurations are designed to work with the log4net.Config.DOMConfigurator and the log4net.Repository.Hierarchy.Hierarchy . These examples are by no means exhaustive configurations for the appenders.

How do I use rolling logging with log4net?

Using rolling files, you tell log4net to split up your files in chunks. Rolling logging can be configured by size, dates, and more. You often see people configuring a log file per day. You can log to elmah.io through log4net too. To do so, install the elmah.io.log4net NuGet package: Configure the elmah.io log4net appender in your config file:

How do I enable log4net in Visual Studio 2017?

Add log4net.config file. Add a new file to your project in Visual Studio called log4net.config and be sure to set a property for the file. Set “Copy to Output Directory” to “Copy Always”. This is important because we need the log4net.config file to be copied to the bin folder when you build and run your app.


2 Answers

The config file is used when you cause a call to the LoggerManager class. This is usually caused by calling LogManager.GetLogger.

See http://logging.apache.org/log4net/release/sdk/log4net.Config.XmlConfiguratorAttribute.html for more details; specifically the note:

Log4net will only look for assembly level configuration attributes once. When using the log4net assembly level attributes to control the configuration of log4net you must ensure that the first call to any of the LoggerManager methods is made from the assembly with the configuration attributes.

like image 66
jedigo Avatar answered Sep 28 '22 00:09

jedigo


A quote from the Apache log4net site:

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. http://logging.apache.org/log4net/release/manual/configuration.html

Here is a good article on how to make sure the configuration is set up and run properly: http://www.fooji.net/blog/post/2010/04/27/logging-log4net-e28093-part-iii.aspx

Basically, you need to either make a logging call (which configures log4net if it isn't already configured) or utilize the DOMConfigurator to manually configure log4net.

like image 24
IAmTimCorey Avatar answered Sep 28 '22 00:09

IAmTimCorey