Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where will log4net create this log file?

When I set the file value to logs\log-file.txt, where exactly will it create this folder? In the /bin directory?

My web.config looks like this:

<log4net>     <appender name="FileAppender" type="log4net.Appender.FileAppender">       <file value="logs\log-file.txt" />       <appendToFile value="true" />       <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />       </layout>     </appender> </log4net> 

Is this the correct way to log:

ILog logger = LogManager.GetLogger(typeof(CCController)); logger.Error("Some Page", ex);  // where ex is the exception instance 
like image 747
Blankman Avatar asked May 12 '10 03:05

Blankman


People also ask

Where do log4net logs go?

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

Where is log4net configuration file?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.

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.


2 Answers

If you want your log file to be place at a specified location which will be decided at run time may be your project output directory then you can configure your .config file entry in that way

<file type="log4net.Util.PatternString" value="%property{LogFileName}.txt" /> 

and then in the code before calling log4net configure, set the new path like below

 log4net.GlobalContext.Properties["LogFileName"] = @"E:\\file1"; //log file path  log4net.Config.XmlConfigurator.Configure(); 

How simple is it? :)

like image 186
Avijit Chatterjee Avatar answered Sep 21 '22 07:09

Avijit Chatterjee


it will create the file in the root directory of your project/solution.

You can specify a location of choice in the web.config of your app as follows:

   <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">       <file value="c:/ServiceLogs/Olympus.Core.log" />       <appendToFile value="true" />       <rollingStyle value="Date" />       <datePattern value=".yyyyMMdd.log" />       <maximumFileSize value="5MB" />       <staticLogFileName value="true" />       <lockingModel type="log4net.Appender.RollingFileAppender+MinimalLock" />       <maxSizeRollBackups value="-1" />       <countDirection value="1" />       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date %-5level [%thread] %logger - %message%newline%exception" />       </layout>     </appender> 

the file tag specifies the location.

like image 31
Oladipo Olasemo Avatar answered Sep 17 '22 07:09

Oladipo Olasemo