log4net

log4net – prosty setup

Posted on

Bibliotekę log4net pobieramy z pomoc Nugeta.

W web.config dodajemy sekcje o log4net do <configSections> oraz konfigurujemy logowanie do pliku “logs\Log.txt”.

<configuration>
   <configSections>
     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
   </configSections>
  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="logs\Log.txt" />
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>
</configuration>

W miejscu gdzie startuje nasz framework (np. Application_Start() w Global.asax, Bootstrapper.ApplicationStartup() dla Nancy) umieszczamy

// Remember about this line
XmlConfigurator.Configure();

// Just check out
ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
logger.Info("Starting");

Często zapomina się o XmlConfigurator.Configure();

Jest to setup minimum, aby tylko zadziałało. Oczywiście polecam się bawić innymi ustawieniami log4neta.

Poziomy logowania

Posted on Updated on

Przyklejone z wiki o log4j:

Level Description
OFF The highest possible rank and is intended to turn off logging.
FATAL Severe errors that cause premature termination. Expect these to be immediately visible on a status console.
ERROR Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
WARN Use of deprecated APIs, poor use of API, ‘almost’ errors, other runtime situations that are undesirable or unexpected, but not necessarily “wrong”. Expect these to be immediately visible on a status console.
INFO Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
DEBUG Detailed information on the flow through the system. Expect these to be written to logs only.
TRACE Most detailed information. Expect these to be written to logs only. Since version 1.2.12.