Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net Not Creating Log File

Tags:

log4net

I'm getting an error on a program that has log4net configured, but no log file is being created. I'm certain the logging is set up in the program because other users have created log files from the same, using an identical config file to what I'm posting below (except for the filepath). I'm sure that I have write permissions to the path. At the point where the program fails, it absolutely must have passed the initialization of the logging.

Does anything look wrong in this config file, or has anybody experienced a similar issue and know something I should look for within the program I'm trying to get a log from?

<?xml version="1.0" encoding="utf-8"?> <configuration>   <configSections>     <section name="AutoTag" type="System.Configuration.NameValueSectionHandler"/>     <section name="WindwardReports" type="System.Configuration.NameValueSectionHandler"/>     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821"/>   </configSections>    <AutoTag>     <add key="_debug" value="true"/>   </AutoTag>    <WindwardReports>     <add key="line.break" value="internal"/>   </WindwardReports>    <appSettings>     <add key="sql.timeout" value="240"/>   </appSettings>    <log4net>     <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">        <param name="File" value="C:\Users\loganm\Documents\Catapult.log"/>        <param name="AppendToFile" value="true"/>       <param name="MaxSizeRollBackups" value="2"/>       <param name="MaximumFileSize" value="100KB"/>       <param name="RollingStyle" value="Size"/>       <param name="StaticLogFileName" value="true"/>       <layout type="log4net.Layout.PatternLayout">         <param name="ConversionPattern" value="%d [%t] ac.server %-5p %c - %m%n"/>       </layout>     </appender>      <root>       <level value="DEBUG"/>       <appender-ref ref="RollingFileAppender"/>     </root>   </log4net> </configuration> 
like image 375
That One Guy Avatar asked Jan 16 '14 00:01

That One Guy


People also ask

Where do log4net logs go?

In your case, the log file will be in bin\Debug\netcoreapp3.

Is log4net structured logging?

log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.

Does log4net create directory?

Log4net watches for any new file created in the folder so simply creating the . log4net configuration file triggers the update within the component that is logging. When using a file appender, the destination folder does not have to exist. Log4net creates the folder.


1 Answers

Give the following code in your application before you put your logging code:

log4net.Config.XmlConfigurator.Configure(); 

You can define it in Global.asax:

void Application_Start(object sender, EventArgs e)  {     // Code that runs on application startup      // Initialize log4net.     log4net.Config.XmlConfigurator.Configure(); } 

You can also add the following line as Kevin advised (either mentioning your config file name or not):

[assembly: log4net.Config.XmlConfigurator] 

or

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

Hope it helps!!!

like image 54
Naren Avatar answered Sep 21 '22 20:09

Naren