Log Management-log4net

Source: Internet
Author: User
Tags log4net

Introduction

The Log4net library is the implementation of the Apache LOG4J Framework on the Micorsoft.net platform, a tool that helps group programmers to output log information to various targets (consoles, files, databases, etc.). (Baidu Encyclopedia)

The actual project uses log4net great convenience program The log information during the operation of the ape record system, especially the BS system is a more practical tool. This article simply explains its use process, is the most basic of the simplest use, no other redundant explanation is simply used.

Steps to use:

Step One:

Create a class library in the project file, this example uses the class library name: Log4netutility. Reference the DLL for the Log4net library, add the profile log4net.cfg.xml, and note that the properties of the file are set to always replicate. The configuration file is a fixed format and can make some simple changes. The basic content is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <section name= "Log4net" Type= "Log4net. Config.log4netconfigurationsectionhandler, log4net "/> </configSections> <!--logging build configuration--<log4net Debug= "true" > <!--Debug Log configuration--<appender name= "Debugappender" type= "log4net. Appender.debugappender "> <layout type=" log4net.      Layout.patternlayout "> <conversionpattern value=" [%date][%thread][%-5level][%c]-%message%newline "/> </layout> </appender> <!--file Log configuration-<appender name= "Rollinglogfileappender" type= "Log4ne      T.appender.rollingfileappender "> <appendtofile value=" true "/> <rollingstyle value=" Date "/> <staticlogfilename value= "false"/> <maxsizerollbackups value= "-1"/> <datepattern value= "Yyyy-MM -dd_hh '. Log ' "/> <lockingmodel value=" log4net. Appender.FileAppender.MinimalLock "/> <fIle value= "log\\"/> <layout type= "log4net. Layout.patternlayout "> <conversionpattern value=" [%date][%thread][%-5level]%message%newline "/> </    Layout> </appender> <!--Setup The root category, add the Appenders and set the default priority--- <root> <level value= "All"/> <appender-ref ref= "Debugappender"/> </root> <l Ogger name= "Fileloglogger" additivity= "false" > <level value= "All"/> <appender-ref ref= "Rollinglogfi Leappender "/> </logger> </log4net></configuration>

Step Two:

Write the log method as follows:

public class Log4netutility {private static ILog FileLogger = Logmanager.getlogger ("Fileloglogger"); private static ILog Debuglogger = Logmanager.getlogger (Methodbase.getcurrentmethod ().        DeclaringType);            Static log4netutility () {string binpath = System.AppDomain.CurrentDomain.BaseDirectory;            String webffilename = BinPath + @ "Bin\config\log4net.cfg.xml";            String appfilename = BinPath + @ "Config\log4net.cfg.xml"; if (file.exists (webffilename)) log4net.            Config.XmlConfigurator.ConfigureAndWatch (New FileInfo (Webffilename)); else if (file.exists (appfilename)) log4net.            Config.XmlConfigurator.ConfigureAndWatch (New FileInfo (Appfilename));        else Console.WriteLine ("Log4net configuration file not found");        public static void Debuglog (String message) {debuglogger.debug (message); } public static void Debug (object o, String message, ExceptiOn exception) {Type type = O.gettype (); Filelogger.debug (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message), exception);            public static void Debug (object o, String message) {Type type = O.gettype (); Filelogger.debug (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message)); public static void Debug (string typeName, String message, Exception Exception) {Filelogger.deb UG (String.        Format ("[{0}]-{1}", TypeName, Message), exception); public static void Debug (string typeName, String message) {Filelogger.debug (string.        Format ("[{0}]-{1}", TypeName, message)); public static void Info (object o, String message, Exception Exception) {Type type = O.gettype (            ); Filelogger.info (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message), exception); } Public Static void Info (object o, String message) {Type type = O.gettype (); Filelogger.info (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message)); public static void Info (string typeName, String message, Exception Exception) {Filelogger.info (String.        Format ("[{0}]-{1}", TypeName, Message), exception); The public static void Info (string typeName, String message) {Filelogger.info (string).        Format ("[{0}]-{1}", TypeName, message)); public static void Warn (object o, String message, Exception Exception) {Type type = O.gettype (            ); Filelogger.warn (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message), exception);            public static void Warn (object o, String message) {Type type = O.gettype (); Filelogger.warn (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message));        }public static void Warn (string typeName, String message, Exception Exception) {Filelogger.warn (string.f        Ormat ("[{0}]-{1}", TypeName, Message), exception); The public static void Warn (string typeName, String message) {Filelogger.warn (string.        Format ("[{0}]-{1}", TypeName, message)); public static void Error (object o, String message, Exception Exception) {Type type = O.gettype            (); Filelogger.error (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message), exception);            public static void Error (object o, String message) {Type type = O.gettype (); Filelogger.error (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message)); public static void Error (string typeName, String message, Exception Exception) {Filelogger.err or (String.        Format ("[{0}]-{1}", TypeName, Message), exception); } PubLic static void Error (string typeName, String message) {Filelogger.error (string.        Format ("[{0}]-{1}", TypeName, message)); public static void Fatal (object o, String message, Exception Exception) {Type type = O.gettype            (); Filelogger.fatal (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message), exception);            public static void Fatal (object o, String message) {Type type = O.gettype (); Filelogger.fatal (String. Format ("[{0}.{ 1}]-{2} ", type. Namespace, type.        Name, message)); public static void Fatal (string typeName, String message, Exception Exception) {Filelogger.fat Al (String.        Format ("[{0}]-{1}", TypeName, Message), exception); The public static void Fatal (string typeName, String message) {Filelogger.fatal (string.        Format ("[{0}]-{1}", TypeName, message)); }    }

Step Three:

Calls within the system:

Log4netutility.log4netutility. Error (this, "Log4net log Error Record");

At this point, Log4net's simple application is finished, this article or that sentence, is to introduce some of its simple usage, tell you how to use it, the specific internal principle of the deeper things or to the later bar.

Log Management-log4net

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.