Create Separate log4net Config File

2/12/2016 4:46:41 PM

Create a separate file for log4net settings. This will allow you to store and deploy the log4net file separately.

//global.asax.cs file
protected void Application_Start()
{	
	log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
}


//log4net.config
<log4net>

  <root>
    <!-- add other appenders here and the log messages will be sent to every listed appender -->
    <appender-ref ref="LogFileAppender" />

    <level value="ALL"/>
  </root>

  <!-- The DebugFileAppender writes all messages to a log file-->
  <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <file value="C:\\Projects\\Logs\\my-website.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c %m%n" />
    </layout>
  </appender>

</log4net>


//testing - homecontroller
private static log4net.ILog _log4net = log4net.LogManager.GetLogger(typeof(HomeController));      

public ActionResult Index()
{
	//testing
	_log4net.Debug("home controller");

	return View();
}