Log4Net full range tracking program

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The original:

1.Log4Net is simple to use

  The use of Log4net must use the log4net.dll file, because log4net is open source, so it can be directly to the source code is added to the project. And then generate a new DLL file.

  The following configuration using a simple file output and the console output.

  The Log4Net configuration file, add the content to a App.config or web.config file:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="D:\a.log"></file>
      <appendToFile value="true"></appendToFile>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%d]:%n%m%n"></conversionPattern>
      </layout>
    </appender>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%d]:%n%m%n"></conversionPattern>
      </layout>
    </appender>
    <root>
      <level value="INFO"></level>
      <appender-ref ref="FileAppender"></appender-ref>
      <appender-ref ref="ConsoleAppender"></appender-ref>
    </root>
    <logger name="MyLogger">
      <level value="ERROR"></level>
      <appender-ref ref="FileAppender"></appender-ref>
      <appender-ref ref="ConsoleAppender"></appender-ref>
    </logger>
  </ log4net>

The structure of 2.Log4Net

Log4net consists of 5 core components: Logger, Appender, Filter, Layout, Object Render.

  Logger:

  Mainly used for record the classification and control log log level. It can be in a variety of formats the output log information, the output level at the same time, it can also control the log. Simple examples above we see the log output, but not output any log results, because we in the log level on the limit.

  Appender:

  Here we simply called "attachment". It means that the log output must depend on it, more specifically to output in the form of text, or the console output format must rely on it to control. Log4net defines a variety of attachment:

  (1) AdoNetAppender will log records to the database. You can use SQL and storage process in two ways .

  (2) AnsiColorTerminalAppender in the ANSI window terminal write high brightness log event.

  (3) AspNetTraceAppender can check the record with asp.net Trace way to log.

  (4) BufferingForwardingAppender to Appenders before the log events in the output cache.

  (5) ConsoleAppender will log output to the console.

  (6) EventLogAppender will log writes Windows Event Log.

  (7) FileAppender log to file.

  (8) MemoryAppender will log stored in the memory buffer.

  (9) NetSendAppender will log output to the Windows Messenger service, these log information will be displayed on the user terminal box.

  (10) RemoteSyslogAppender through the UDP network protocol log to Remote syslog service.

  (11) RemotingAppender through the.NET Remoting log to the remote receiver.

  (12) RollingFileAppender will log to roll back the documents in the form of written to a file.

  (13) SmtpAppender will log writes the mail.

  (14) TraceAppender will log onto.NET trace system.

  (15) UdpAppender will log connectionless UDP datagrams form sent to the remote host or in the form of UdpClient broadcast.

  Filter:

  Appender is the output log to the default mode, meaning that in accordance with the default value of output. While the Filter mean filter, Filter can be used to log in a different format to the output.

  Layout:

  This is used to control the log output display format.

  Object Render:

  According to user defined criteria to the output log.

  The Log4net structure (coming from other places.)

  

The log object manager LogManager:LogManager Log4net is used to manage all the Logger objects, GetLogger () can be used to find the existing Logger object if the object does not exist, it will automatically create a Logger object, and manage it.

3.Log4Net log level

Log4net can be divided into the following grades:

  FATAL destruction level

  ERROR error level

  WARN warning level

  The INFO message level

  DEBUG debug level

  The log level: FATAL> ERROR> WARN > INFO > DEBUG.

  Only the log output level is greater than or equal to the configuration level to output log information.

  For example, log configuration level I is INFO, then only log.Info (), log.Warn (), log.Error (), log.Fatal () to output log information, the Debug mode can not output.

  Log4net has two kind of special configuration level: ALL - allows the log level output of all, OFF refused the log level output of all.

The 4.Log4Net configuration description

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
    <!--Log records to the database-->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="100" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="data source=GAS-HECHEN;initial catalog=dsc141051_db;integrated security=false;persist security info=True;User ID=sa;Password=000000" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
    
    <!--Enter the log to the console-->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[time]:%d%n[grade]:%p%n[content]:%m%n%n"></conversionPattern>
      </layout>
    </appender>

    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <param name="file" value="D:\a.log"></param>
      <param name="appendToFile" value="true"></param>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[time]:%d%n[grade]:%p%n[content]:%m%n%n"></conversionPattern>
      </layout>
    </appender>
    
    <root>
      <level value="INFO"></level>
      <appender-ref ref="FileAppender"></appender-ref>
      <appender-ref ref="ConsoleAppender"></appender-ref>
    </root>
    <logger name="MyLogger">
      <level value="INFO"></level>
      <appender-ref ref="FileAppender"></appender-ref>
      <appender-ref ref="ConsoleAppender"></appender-ref>
    </logger>
</log4net>

This configuration file defines three different Appender, are recorded by ADO.NET data, ConsoleAppender from the console output, FileAppender log file output.

  However, the configuration of the Log4Net attachment is far more than these, here to upload a comparison of all attachment configuration file for your reference Click to download.

  According to the log configuration file above still use the following code to test:

  

Note here, log4net.Config.XmlConfigurator.Configure(); This is equivalent to initialize Log4net, if it is in the Web program, We can put this sentence is added to the Global.asax file of the Application_Start (object sender, EventArgs E) method, When the application starts up initialization configuration information.

The two configuration:

  

Here root and logger configuration node, root is equivalent to the default log object configuration, and logger is the specific log configuration. They can coexist.

Both level and appender-ref node to the two node, respectively, for the specified output level log and references which attachment. Each log object can use multiple appender, here the configuration and use the file output and the console output.


  

The above procedure declares two log object log and Log1, Log is present in the initialization time, It is <logger name= "MyLogger" > this log object, While Log1 is not present in the LogManager log object, Here it will re create a and managed by LogManager, Again behind the use of the existing object using. Here two log object and use the results are as follows:

  

The log file output level now I modify the configuration file:

<logger name="MyLogger">
<level value="WARN"></level>
<appender-ref ref="FileAppender"></appender-ref>
<appender-ref ref="ConsoleAppender"></appender-ref>
<appender-ref ref="AdoNetAppender"></appender-ref>
</ logger>

  The operation results in Figure:

  

Apparently MyLogger object no log output log information.

The six Log4Net parameters

  A similar%m%n such symbols above we see configuration files, these symbols are of special significance.

%m[%message] : The log message output
%N: Wrap
%d[%datetime] : Output current statement running time
%R: output program from running to the execution to the consumption of the current statement of the number of milliseconds
%d : The current statement where the thread ID
%P: the current priority level log
%c : Name of the current log object
%L: the line number where the output statement
%F : The file name output statements
%-Digital: the minimum length of the item, if not, is padded with spaces

Seven custom lightweight log system

  Specific reference to the article: ORM mapping framework summary -- the log processing.

  There is no longer much about, The sample download.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Abigail at August 31, 2014 - 10:25 AM