[Improvement] Use Log4net to create a log record and log4net to create a log record

Source: Internet
Author: User

[Improvement] Use Log4net to create a log record and log4net to create a log record

In the previous article, only common file reads and writes are used for log writing. As many friends have said, frequent file reads and writes may cause many problems, the code lacks boundary control and operation control, and does not manage resources. It is a typical bad code.

As mentioned by our predecessors, Log4net is used to write logs, which is very convenient and integrates log control, reduce a series of unexpected errors caused by log writing.

 

There are also a lot of tutorials on the Internet. In combination with my lessons, I am also shamelessly posting my own implementation steps. Thank you !!!

 

The app. config configuration file is directly written in the app. config file of the system,Creating a config file will be unrecognizable and I don't know why. Could you advise me?

 

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <configSections> <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> </configSections> <log4net> <root> <level value =" INFO "/> <! -- Log recorded as a file --> <appender-ref = "RollingLogFileAppender"/> </root> <appender name = "RollingLogFileAppender" type = "log4net. appender. rollingFileAppender "> <! -- Log location --> <file value = "Log \"/> <! -- Log name --> <datePattern value = "yyyy-MM-dd'.txt '"/> <! -- Minimum thread lock --> <lockingModel type = "log4net. Appender. FileAppender + MinimalLock"/> <! -- Append to file --> <appendToFile value = "true"/> <! -- Slide log --> <RollingStyle value = "Date"/> <! -- Unfixed log name --> <staticLogFileName value = "false"/> <! -- Number of backup logs --> <param name = "MaxSizeRollBackups" value = "100"/> <! -- Log format --> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" record time: [% date] [% t] %-5 p % n-% m % n "/> </layout> </appender> </log4net> <! -- The program comes with the startup configuration file --> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.0"/> </startup> </configuration>

The preceding configuration creates a log file every day, which I think is convenient for management and viewing.

 

Next, we will create a public class for logging for other operations to use our logging.

Using System; using System. reflection; using log4net; using log4net. config; [assembly: XmlConfigurator (Watch = true)] namespace DataBindingDemo. helper {public static class LogHelper {/// <summary> /// record the error log /// </summary> /// <param name = "ex"> </param> public static void LogError (Exception ex) {var type = MethodBase. getCurrentMethod (). declaringType; var log = LogManager. getLogger (type); log. error (ex );} /// <summary> /// record common logs /// </summary> /// <param name = "info"> </param> public static void LogInfo (string info) {var type = MethodBase. getCurrentMethod (). declaringType; var log = LogManager. getLogger (type); log. info (info );}}}

Here, I only use two log formats.

If necessary, you can make adjustments as needed.

Problem:The type in LogManager. GetLogger (type); cannot be null. However, the method type obtained through reflection will only be reflected to the previous layer and not to the called method layer. That is to say, type is almost useless here. No detailed explanation was found at the beginning of the online. Ask your predecessors for advice.

 

Usage

LogHelper. LogInfo ("record the first log ");

In this way, we can record the logs we need.

 

Question: You can only record various logs in one folder through configuration. If you want to classify logs, different logs, Key logs, and error logs, they will be recorded in different folders, I can't think of how to implement it in Log4net. Please give me some advice!

Thank you!

 

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.