"Improved" to create a log record with log4net

Source: Internet
Author: User
Tags log4net

The previous essay only used ordinary file read and write to write the log, as many friends said, frequently read and write files will cause a lot of problems, code lack of border control and operation control, no management of resources, is a very typical bad code.

Then through the predecessors of the point, today using the Log4net log write, found very convenient, but also integrates the control of the log, reducing the number of errors due to log write.

There are many tutorials on the internet, in this, combined with my lessons, I also have the audacity to post their own implementation steps, welcomed the predecessors to correct!!!

App. Config profile, I am directly in the system's app. config file, the new config configuration file will not be recognized, and do not know why, ask the elder advice

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <configsections>        < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler, log4net "/>    </configsections>    <log4net>        <Root>            < Levelvalue= "INFO"/>            <!--record log as file Form -            <Appender-refref= "Rollinglogfileappender"/>        </Root>        <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender ">            <!--Log Location -            <filevalue= "log\\" />            <!--journal name -            <Datepatternvalue= "Yyyy-mm-dd '. txt '"/>            <!--Minimum thread lock -            <Lockingmodeltype= "log4net." Appender.fileappender+minimallock "/>            <!--Append to File -            <Appendtofilevalue= "true"/>            <!--Sliding log logging -            <Rollingstylevalue= "Date"/>            <!--non-fixed log name -            <Staticlogfilenamevalue= "false"/>            <!--Number of backup logs -            <paramname= "Maxsizerollbackups"value= "+"/>            <!--Log Format -            <Layouttype= "log4net." Layout.patternlayout ">                <Conversionpatternvalue= "Recording time:"%date "[%t]%-5p%n-%m%n"/>            </Layout>        </Appender>    </log4net>    <!--program comes with a startup configuration file -    <Startup>        <supportedruntimeversion= "v4.0"SKU=". netframework,version=v4.0 "/>    </Startup></Configuration>

The above configuration will create a new log file every day, I think this will be easy to manage, but also easy to view.

Next, we create a new public class for logging, for other operations to use our log records.

usingSystem;usingSystem.Reflection;usinglog4net;usinglog4net. Config; [Assembly:xmlconfigurator (Watch=true)]namespacedatabindingdemo.helper{ Public Static classLoghelper {/// <summary>        ///Logging error Logs/// </summary>        /// <param name= "ex" ></param>         Public Static voidLogError (Exception ex) {varType =Methodbase.getcurrentmethod ().            DeclaringType; varLog =Logmanager.getlogger (type); Log.        Error (ex); }        /// <summary>        ///Log Normal logs/// </summary>        /// <param name= "info" ></param>         Public Static voidLoginfo (stringinfo) {            varType =Methodbase.getcurrentmethod ().            DeclaringType; varLog =Logmanager.getlogger (type); Log.        info (info); }    }}

In this case, I only use the record format of the two logs.

Students who are in need can make adjustments according to their needs.

Problem: The type in Logmanager.getlogger (type) cannot be null, but by reflection to get the method type I will only reflect to the previous layer and will not be reflected to the layer that called the method. That means the type here is almost useless. On the Internet at the beginning of the search and did not find the corresponding detailed explanation. Seek the guidance of predecessors.

How to use

Loghelper.loginfo (" record the first log ");

This will keep a record of the logs we need.

Question: Through the configuration can only be a variety of log records in a folder under, if I want to classify the log, different logs and key log, error log, recorded in a different folder, in the log4net unexpectedly how to achieve, ask predecessors again guidance!

Kneel, thank you!

Improve to establish logging with 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.