Log4net entry (HELP) and log4net entry help

Source: Internet
Author: User

Log4net entry (HELP) and log4net entry help

In the previous sections of the Log4net getting started file, we need to write the following code in the log4net class:

1 private static log4net. ILog log = log4net. LogManager. GetLogger (System. Reflection. MethodBase. GetCurrentMethod (). DeclaringType );

If multiple classes use log4net to output log information, write this line of code in each class. To avoid this situation, we will write a static class to encapsulate log4net. In the future, we need to output the log information somewhere. We only need to call the corresponding method of this static class. The implementation steps are as follows:

1. Create a blank solution named "Utils ".

2. Create a class library project named "Log4netUtil" in this solution, and then use "NuGet Package Manager" to install the log4net assembly for this class library project.

3. Expand the "Properties" attribute of the "Log4netUtil" Class Library Project, double-click the "AssemblyInfo. cs" file, and add the following code at the bottom of the file:

1 [assembly: log4net. Config. XmlConfigurator (ConfigFile = "Log4net. config", Watch = true)]

Note: The "Log4net. config" in the above line of code must be the same as the name of the configuration file we added later.

4. Add a static class named "Log4netHelper" to the "Log4netUtil" Class Library Project. The Code is as follows:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Log4netUtil 8 { 9     public static class Log4netHelper10     {11         private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);12 13         public static void Debug(object message)14         {15             log.Debug(message);16         }17 18         public static void Debug(object message, Exception exception)19         {20             log.Debug(message, exception);21         }22 23         public  static void Info(object message)24         {25             log.Info(message);26         }27 28         public static void Info(object message, Exception exception)29         {30             log.Info(message, exception);31         }32 33         public static void Warn(object message)34         {35             log.Warn(message);36         }37 38         public static void Warn(object message, Exception exception)39         {40             log.Warn(message, exception);41         }42 43         public static void Error(object message)44         {45             log.Error(message);46         }47 48         public static void Error(object message, Exception exception)49         {50             log.Error(message, exception);51         }52 53         public static void Fatal(object message)54         {55             log.Fatal(message);56         }57 58         public static void Fatal(object message, Exception exception)59         {60             log.Fatal(message, exception);61         }62     }63 }

5. Right-click the "Utils" solution and create a new console application named "Log4netTest". This application is mainly used to test whether the log4net help class runs correctly.

6. Right-click the "Reference" node of the newly created "Log4netTest" console application and add a reference to the "Log4netUtil" class library.

7. Add a file named "Log4net. config, set the value of the "Copy to output directory" attribute of the configuration file to "always copy", and then modify the content of the configuration file as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> 5 </configSections> 6 7 <log4net> 8 <! -- Write the log to the file as a rollback file --> 9 <! -- Split the log file by date and use the date as the log file name --> 10 <appender name = "RollingFileAppenderNameByDate" type = "log4net. Appender. RollingFileAppender"> 11 <! -- Log file storage location, which can be an absolute or relative path --> 12 <file value = "C: \ Logs \"/> 13 <! -- Append log information to an existing log file --> 14 <appendToFile value = "true"/> 15 <! -- Minimum lock mode, which allows multiple processes to write data to the same file --> 16 <lockingModel type = "log4net. Appender. FileAppender + MinimalLock"/> 17 <! -- Specify to split log files by Date --> 18 <rollingStyle value = "Date"/> 19 <! -- Log File naming rules --> 20 <datePattern value = "& quot; UtilLogs _ & quot; yyyyMMdd & quot;. log & quot;"/> 21 <! -- When the date is used as the name of the log file, the value of staticLogFileName must be set to false --> 22 <staticLogFileName value = "false"/> 23 24 <layout type = "log4net. layout. patternLayout "> 25 <conversionPattern value =" % date [% thread] %-5 level % logger-% message % newline "/> 26 </layout> 27 </appender> 28 29 <root> 30 <! -- Control level, from low to high: ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF --> 31 <! -- For example, if the definition level is INFO, the level of INFO is down. For example, the DEBUG log will not be recorded --> 32 <! -- If no level value is defined, the default value is DEBUG --> 33 <level value = "ALL"/> 34 <! -- Split log files by date, use the date as the log file name --> 35 <appender-ref = "RollingFileAppenderNameByDate"/> 36 </root> 37 </log4net> 38 </configuration>

8. Double-click to open the "Program. cs" file in the "Log4netTest" project and modify the file as follows:

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Log4netTest 8 {9 class Program10 {11 static void Main (string [] args) 12 {13Log4netUtil. Log4netHelper. Info ("Info! ");14} 15} 16}

Run the console application and we can see that the log file is correctly generated!

Source code download

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.