Log4net for Windows form program

Source: Internet
Author: User
Tags log4net connectionstrings

Reference: http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html

 

Today, I have been away from my company for more than a week. I used log4net to provide the log function to improve my own systems.

1. Add reference

Add a log4net (download: http://logging.apache.org/log4net/download.html) reference and reference the DLL or source code project.

2. modify or add app. config

By default, an app. config file is added to the project.

After the program is compiled, A <your Set Name>. EXE. config file is generated under your execution Directory, which is the same as the content of this file.

3. Edit the app. config file

Add the red part as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<! -- Serialize object parameters -->
<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net-net-1.0"/>
</Configsections>

<! -- Database connection string -->
<Connectionstrings>
<Add name = "connstr" connectionstring = "Server = 130.251.9.132; database = roommanage; uid = sa; Pwd = sa123;" providername = "system. Data. sqlclient"/>
</Connectionstrings>

<! -- Parameters required for system initialization -->
<Deleetask>
<Add key = "conn" value = "Property Test"/>
<Add key = "user" value = "username, ID"/>
</Appsettings>

 

<! -- Log4net configuration parameter file -->
<Log4net>
<Root>
<Level value = "all"/>
<Appender-ref = "rollingfileappender"/>
</Root>

<Appender name = "rollingfileappender" type = "log4net. appender. rollingfileappender">
<! -- Log file name -->
<File value = "/log/roommanage. log"/>
<! -- Whether to append an object -->
<Appendtofile value = "true"/>
<! -- Change the log file according to the file size -->
<Rollingstyle value = "size"/>
<! -- Maximum number of transformations -->
<Maxsizerollbackups value = "5"/>
<! -- Maximum file size -->
<Maximumfilesize value = "10 MB"/>
<! -- Whether the log file name is static -->
<Staticlogfilename value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% date [% thread] %-5 level % logger [% property {NDC}]-% message % newline"/>
</Layout>
</Appender>
</Log4net>

</Configuration>

Note that the <configuration> and <deployments> labels must be fewer.

4. Edit the Assembly. CS File

Add the following content:
[Assembly: log4net. config. domconfigurator (configfileextension = "Config", watch = true)]

5. Add the following code to the program:
Log4net. ilog log = log4net. logmanager. getlogger ("loginlogger ");
Log. debug ("Login succed! ");

Loginlogger can differentiate the log information of different functions. The name can be changed! For more information about the parameter meanings, see the official example.

PS:

This is the simplest log. I implement five logs (each 10 MB) for reading and writing in turn. When the logs are full, the next log is fully written and then the logs are written from the first log, similar to writing redo log files in non-archive mode in Oracle.

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.