Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you have in your log4net config? Hacks, optimizations, observations?

This is my log4net config file

<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="true">
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="DEBUG" />
      <levelMax value="FATAL" />
    </filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %5rms [%-17.17t] %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>

  <appender name="SmtpAppender" type="MySmtpClientSmtpAppender">
    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject value="Game Server Monitoring Failed!" />
    <smtpHost value="smtp.x.com" />
    <username value="admin" />
    <password value="x" />
    <bufferSize value="256" />
    <lossy value="true" />
    <evaluator type="log4net.spi.LevelEvaluator">
      <threshold value="ERROR" />
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d [ThreadId: %t] Class:%c{1} Method:%M %nMESSAGE:%n%m%n%n" />
    </layout>
  </appender>

  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\\wwwlog" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />
    <maximumFileSize value="10MB" />
    <!--<rollingStyle value="Date" />-->
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
    </layout>
  </appender>


  <root>
    <level value="DEBUG" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="MySmtpAppender" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

Am I missing anything? Any tips/ advice to decrease log size while maintaining clarity, better conversion patterns? Any interesting hacks?

Edit: XMl file didnt get into the code block.Updating. Any sugggestions?

like image 683
Cherian Avatar asked Mar 06 '09 16:03

Cherian


People also ask

What are the main components of log4net?

Log4net has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

What is log4net config?

The log4net configuration can be configured using assembly-level attributes rather than specified programmatically. If specified, this is the filename of the configuration file to use with the XmlConfigurator. This file path is relative to the application base directory (AppDomain. CurrentDomain.

What is the use of log4net?

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.

Where are log4net logs stored?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation. The different log files are described in Services logs.


2 Answers

Nobody mentioned encoding. It's pretty important if you are logging messages in Chinese, Japanese, Arabic or any double-byte language. Here is an example:

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <encoding value="utf-8" />
  <!-- etc. -->
</appender>
like image 143
nakhli Avatar answered Oct 21 '22 09:10

nakhli


One suggestion for the RollingLogFileAppender is to use the minimal lockingModel. With this setting the log file isn't locked, rather it is opened and then closed for each write.

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
like image 25
Dscoduc Avatar answered Oct 21 '22 07:10

Dscoduc