Where will log4net create this log file?

Blankman picture Blankman · May 12, 2010 · Viewed 144.3k times · Source

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

Answer

Avijit Chatterjee picture Avijit Chatterjee · Jul 19, 2013

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? :)