Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get log4net log file in C#

Tags:

c#

.net

log4net

This is my configuration for log4net:

<log4net>     <appender name="MyLogger" type="log4net.Appender.RollingFileAppender">         <file value="MyLog.log" />         <appendToFile value="true" />          <rollingStyle value="Size"/>         <maxSizeRollBackups value="20"/>         <maximumFileSize value="1000KB"/>         <layout type="log4net.Layout.PatternLayout">             <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss},%p,%m%n" />         </layout>     </appender>     <root>         <level value="DEBUG" />         <appender-ref ref="MyLogger" />     </root> </log4net> 

In C# I'm trying to get the name of the log file (which is MyLog.log). I googled and tried many things but failed to do so. Any help?

Thanks!

like image 483
Carlo Avatar asked Aug 27 '09 21:08

Carlo


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.

How do I get log4net DLL?

You can download the desired Log4net. dll from this path: http://logging.apache.org/log4net/download.html, Whilst I've also attached the required Log4net.

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

Solution is quite easy in your situation; just use this code:

var rootAppender = ((Hierarchy)LogManager.GetRepository())                                          .Root.Appenders.OfType<FileAppender>()                                          .FirstOrDefault();  string filename = rootAppender != null ? rootAppender.File : string.Empty; 
like image 127
Yakeen Avatar answered Sep 18 '22 17:09

Yakeen