Use Log4Net to record custom attributes to a file (3)

Source: Internet
Author: User
Tags log4net

 

Use Log4Net to record custom attributes to a file (3)

 

This solves the problem of logging custom attributes to the database. A new idea emerged. Can custom attributes also be recorded in files? The answer is yes, because since Log4Net has considered the database record method, of course, it must also consider other record methods. But how, how, and how (this sentence comes from movie kung fu pandatv ). Next, let's take a look at how to record custom attributes to a file.

 

Before serving the food, I would like to make a small supplement. First, the message carrier we use is still LogMessage, And the LogMessage code can be viewed. "Log4Net is also used to record the log configuration to the database. (1) ". The name of the custom attribute is still UserID. Of course, you can output more custom attributes and add them by yourself. The background code should still be my previous article "log recorded to the background of the database using Log4Net (ii )". Is it good? The same code can support multiple log output methods, which is one of the advantages of Log4Net.

 

Okay, serving:

 

    <logger name="MisTxtLogger">      <level value="ALL"/>      <appender-ref ref="MisTxtFile" />    </logger>

 

<! -- Txt record format --> <! -- Information log configuration --> <appender name = "MisTxtFile" type = "log4net. appender. rollingFileAppender "> <param name =" File "value =" D: \ MisLog \ "/> <param name =" AppendToFile "value =" true "/> <param name =" MaxFileSize "value =" 10240 "/> <param name = "MaxSizeRollBackups" value = "100"/> <param name = "StaticLogFileName" value = "false"/> <param name = "DatePattern" value = "yyyyMMdd"/> <param name = "RollingStyle" value = "Date"/> < ! -- Custom member --> <parameter> <parameterName value = "@ UserID"/> <dbType value = "Int32"/> <layout type = "JJ. data. logCommon. customLayout "> <conversionPattern value =" % UserID "/> </layout> </parameter>
<Layout type = "JJ. data. logCommon. customLayout "> <param name =" ConversionPattern "value =" % n log time: % d [% t] % n log level: %-5 p % n log class: % c % n % m % n Message description: % UserID % n "/> </layout>
</Appender>

  

 

In the background code: change the name of the loaded configuration node to "MisTxtLogger" to apply the text output configuration.

 

    _log = log4net.LogManager.GetLogger("MisTxtLogger"); 

  

This is the configuration in the form of file records. You can put it in the <log4net> </log4net> configuration file. The following configuration is taken from the preceding configuration for convenience.

 

<! -- Custom member --> <parameter> <parameterName value = "@ UserID"/> <dbType value = "Int32"/> <layout type = "JJ. data. logCommon. customLayout "> <conversionPattern value =" % UserID "/> </layout> </parameter> <layout type =" JJ. data. logCommon. customLayout "> <param name =" ConversionPattern "value =" % n log time: % d [% t] % n log level: %-5 p % n log class: % c % n % m % n Message description: % UserID % n "/> </layout>

  

 

In the text record format, you still need to process the Custom Attributes first. The "UserID" in the Code is the custom attribute in this example. Then, let's look at its input method:

 

<Layout type = "JJ. data. logCommon. customLayout "> <param name =" ConversionPattern "value =" % n log time: % d [% t] % n log level: %-5 p % n log class: % c % n % m % n Message description: % UserID % n "/> </layout>

  

 

Message description: % UserID. In Log4Net, % UserID represents the value of the custom attribute. Of course, % n is a line feed, and % m is equivalent to % message, that is, the message content. I will not explain the other % elements one by one. You can find details on the Internet.

Well, according to the above configuration, the custom attributes can be output to the file now, isn't it easy.

 

Note: You must be careful when configuring the log. In many cases, the log cannot be output properly because it is not due to code problems, but often because of small errors in the configuration, therefore, I have always thought that Log4Net configuration is a terrible thing. If you accidentally configure the Log4Net configuration, you will not be able to record the logs normally. However, its configuration is clear and easy to understand. Don't worry about it at the beginning. Let's take a look at the meanings of Layout, appendar, and other configuration sections, and then read the Log4Net configuration file. You will suddenly feel open.

 

Now, let's write it here. I'm bailing.

 

 

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.