skynyrd
11/1/2016 - 8:26 PM

Log4net configuration

Log4net configuration

Add to your AppConfig ( must be first element after ):

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net configSource="log4net.config"></log4net>

And sample log4net.config (Do not forget to mark as "Copy Always"):

<log4net>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="C:\\logs\\YourServiceName\\YourServiceName.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender" />
    <appender-ref ref="ConsoleAppender" />
  </root>
</log4net>

Configure when your app starts:

XmlConfigurator.Configure();

Use logger in your class:

private readonly ILog logger = LogManager.GetLogger(typeof(NancySelfHost));
.
.
.
logger.InfoFormat("Service is starting on " + settings.Url);