Log4net does not write to file

I want to add a new file to a file. This is my 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="test"/> </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="All"/> <appender-ref ref="RollingFileAppender"/> </root> 

and in my class add

 [assembly: XmlConfigurator(Watch = true)] 

and I add access to everyone for the file, but: log4net does not write to the file. Why?

+7
c # log4net
source share
4 answers

Log4net fails when a problem occurs. Constructive vanity is that no registration is preferred to uninstall the application. To find out what is wrong, enable Log4net internal debugging by adding this key to your .config [app / web] file:

 <appSettings> <add key="log4net.Internal.Debug" value="true"/> </appSettings> 

Debug messages will be written to the console or to the System.Diagnostics.Trace system. Read more from Phill Haack at http://haacked.com/archive/2006/09/26/Log4Net_Troubleshooting.aspx/

There are several reasons why a Log4net error can occur. Problems with permissions in the log file directory for starters (especially for server processes, where it most likely works under a limited set of security permissions).

+23
source share

You just need to call Configure:

 log4net.Config.XmlConfigurator.Configure(); 

Here you can see more detailed information: Log4net does not write a log file

+2
source share

You need to initialize the registration as the very first step in your application and from the same assembly as the [assembly] tag:

From docs :

Therefore, if you use configuration attributes, you must call log4net so that it can read the attributes. A simple call to LogManager.GetLogger will cause attributes to appear on the calling assembly for reading and processing. Therefore, it is necessary to make a logging call as early as possible during the launch of the application and of course, before any external assemblies have been loaded and called.

Add something like this to your startup code:

 LogManager.GetLogger("Initialises logging from assembly attributes"); 
0
source share

You should add this configuration section:

 <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> 

for a reference to the log4net configuration.

0
source share

All Articles