Log Management-Log4net, log-log4net

Source: Internet
Author: User
Tags log4net

Log Management-Log4net, log-log4net

Introduction

The log4net library is implemented by the Apache log4j framework on the Micorsoft. NET platform. It is a tool that helps programmers to output log information to various targets (such as the console, files, and databases. (Baidu encyclopedia)

In actual projects, log4net is a very convenient tool for programmers to record the log information during system operation, especially for bs systems. This article briefly explains how to use it. It is the most basic and simple application. No additional explanation is just a simple use.

 

Procedure:

Step 1:

Create a class library in the project file. In this example, the class library name is Log4NetUtility. REFERENCE The log4net library dll and add the configuration file log4net. cfg. xml. Note that you must set the attribute of this file to always copy. The configuration files are in some fixed formats and can be modified. 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 configuration setup --> <log4net debug = "true"> <! -- Debug log configuration --> <appender name = "DebugAppender" type = "log4net. appender. debugAppender "> <layout type =" log4net. layout. patternLayout "> <conversionPattern value =" [% date] [% thread] [%-5 level] [% c]-% message % newline "/> </layout> </ appender> <! -- File log configuration --> <appender name = "RollingLogFileAppender" type = "log4net. 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. patternLay Out "> <conversionPattern value =" [% date] [% thread] [%-5 level] % message % newline "/> </layout> </appender> <! -- Setup the root category, add the appenders and set the default priority --> <root> <level value = "ALL"/> <appender-ref = "DebugAppender"/> </root> <logger name = "FileLogLogger" additivity = "false"> <level value = "ALL"/> <appender-ref = "RollingLogFileAppender"/> </logger> </log4net> </ configuration>

 

Step 2:

The log writing method is 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 = o. getType (); fileLogger. debug (string. format ("[{0 }. {1}]-{2} ", type. namespace, type. name, message), exception);} public static void Debug (object o, String message) {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. debug (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 = 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);} 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. format ("[{0}]-{1}", typeName, message), exception);} 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 = 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. error (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 = 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. fatal (string. format ("[{0}]-{1}", typeName, message), exception);} public static void Fatal (string typeName, String message) {fileLogger. fatal (string. format ("[{0}]-{1}", typeName, message ));}}

 

Step 3:

Calls within the system:

Log4NetUtility. Log4NetUtility. Error (this, "log4net log Error log ");

 

At this point, the simple application of log4net has been introduced. In this article, we will introduce some simple usage of log4net and show you how to use it, the specific internal principles of this solution are more profound.

 

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.