Go Log4net Log Plugin configuration detailed

Source: Internet
Author: User
Tags log4net

Log4net is an excellent third-party log framework that can easily be loaded into development projects (referencing Log4net DLLs, and then configuring some basic parameters), helping programmers to output log information to a variety of different goals, common with text, database, Windows logs, mail, Memory buffers and other targets.

First need log4net DLL (source DLL version is 1.2.10.0), refer to the program, and then configure the following configuration file, the configuration file is as follows:

[HTML]View PlainCopy
  1. <? XML version="1.0"?>
  2. <configuration>
  3. <configsections>
  4. < section name="log4net"
  5. type="log4net. Config.log4netconfigurationsectionhandler,log4net "/>
  6. </configsections>
  7. <!--site Log configuration section --
  8. <log4net>
  9. <root>
  10. <!--control level, from low to High: all| debug|info| warn| error| fatal| Off-->
  11. <!--such as the level of info defined, the level below the info level, such as the debug log will not be logged -
  12. <!--If a value of level is not defined, the default is Debug-->
  13. <level value="ERROR"/>
  14. <appender-ref ref="Rollingfileappender"/>
  15. </root>
  16. <appender name="Rollingfileappender" type= "log4net. Appender.rollingfileappender ">
  17. <!--log file name begins --
  18. <file value="C:\Log\TestLog4net.TXT"/>
  19. <!--multi-threaded with minimal lock- in
  20. <lockingmodel type="log4net. Appender.fileappender+minimallock "/>
  21. <!--date format, change every day for a file record, if not set to always log only one day, you need to set --
  22. <datepattern value="(yyyyMMdd)"/>
  23. <!--append to file, default to True, usually without setting --
  24. <appendtofile value="true"/>
  25. <!--transform in the form of a date, in which case there is only one log per day --
  26. <!--node settings for maxsizerollbackups and maximumfilesize are not meaningful at this time-
  27. <!--<rollingstyle value= "Date"/>-->
  28. <!--transform in the form of log size--
  29. <!--In this case, the node settings for maxsizerollbackups and maximumfilesize are meaningful--
  30. <rollingstyle value="Size"/>
  31. <!--the number of log files logged per day, used in conjunction with maximumFileSize
  32. <maxsizerollbackups value="ten"/>
  33. <!--The maximum size of each log file-
  34. <!--available units: kb| Kb: Gb-->
  35. <!--do not use decimals, otherwise it will always be written to the current log--
  36. <maximumfilesize value="2MB"/>
  37. <!--log format --
  38. <layout type="log4net. Layout.patternlayout ">
  39. <conversionpattern value="%date [%t]%-5p%c-%m%n"/>
  40. </Layout>
  41. </appender>
  42. </log4net>
  43. </configuration>

Then the code looks like this:

[CSharp]View PlainCopy
  1. Using System;
  2. Using System.Reflection;
  3. Using Log4net;
  4. Using Log4net. Config;
  5. Namespace log4net1001
  6. {
  7. Class Program
  8. {
  9. static void Main (string[] args)
  10. {
  11. Console.WriteLine ("Begin Run");
  12. Xmlconfigurator.configure ();
  13. Type type = Methodbase.getcurrentmethod (). DeclaringType;
  14. ILog M_log = Logmanager.getlogger (type);
  15. M_log.  Debug ("This is a debug log");
  16. M_log.  Info ("This is an info log");
  17. M_log.  Warn ("This is a Warn log");
  18. M_log.  Error ("This is an error log");
  19. M_log.  Fatal ("This is a Fatal log");
  20. Console.WriteLine ("End");
  21. Console.ReadLine ();
  22. }
  23. }
  24. }

Run the results and write the following information in the C:\Log\TestLog4net.TXT file:
2013-10-01 22:35:53,793 [10]error Log4net1001.program-This is an ERROR log
2013-10-01 22:35:53,802 [10]fatal Log4net1001.program-This is a FATAL log

The configuration description of the layout node:
%m (message): the output log message;
%n (newline): line change;
%d (datetime): Outputs the time at which the current statement is run;
%r (Runtime): The number of milliseconds that the output program consumes from running to executing to the current statement;
%t (ThreadID): The thread ID where the current statement resides;
%p (priority): The current log level of the log;
%c (Class): The name of the current log object;
%l: The line number where the output statement is located;
%F: The file name where the output statement resides;
%-10: Indicates the minimum length is 10, if not enough, then fill with a space;

2. When the configsections node is not placed on the first node under the configuration node, no warning is reported at compile time, but the runtime does not log. This is because the custom node configsections this must be placed first, which is the syntax of the app. config file;

3. For a large number of logging programs, be aware that the configuration Rollingstyle value is composite, and set the maxsizerollbackups (number of log files logged per day) and maximumFileSize (maximum size per log file). I've had a log once too big problem, one version of the temporary release has changed a method without testing, resulting in dozens of G log per day, a few days to fill the server disk.

4. When the discovery does not remember the log, may investigate the question from two aspects, one is log4net has not initialized, namely Xmlconfigurator.configure (); second, look at the level node configuration is not correct;

5. When you find that some of the settings parameters do not work, please check that the same node has been set several times, when set repeatedly, Log4net will choose the last one as the actual setting. For example, set the logging level setting to two times such as <level value= "ERROR"/><level value= "All"/>, then end up with all as the logging level.

6. If this is a Web site project, you must add it in the last line of the AssemblyInfo.cs file in your project:

[Assembly:log4net. Config.domconfigurator (configfile = "Web. config", Watch = true)]

This article goes to: http://blog.csdn.net/pfe_nova/article/details/12225349

Go Log4net Log Plugin configuration detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.