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.