Log4net learning records operation logs

Source: Internet
Author: User

To begin.

Log4net configuration file

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <log4net>
3 <! -- Define output to file -->
4 <appender name = "logfileappender" type = "log4net. appender. fileappender">
5 <! -- If the file storage location is not specified, the file is saved in the root directory of the Project. -->
6 <file value = "log4dao.txt"/>
7 <! -- Whether to overwrite; default value: True -->
8 <appendtofile value = "true"/>
9 <rollingstyle value = "date"/>
10 <datepattern value = "yyyymmdd-hh: mm: SS"/>
11 <layout type = "log4net. layout. patternlayout">
12 <! -- Text description at the end of each log -->
13 <footer value = "by xqh"/>
14 <! -- Output format -->
15 <conversionpattern value = "record time: % date Log Level: %-5 level operation class: % logger-operation status: % message % newline"/>
16 </layout>
17 </appender>
18
19 <root>
20 <! -- <Level value = "Warn"/> -->
21 <! -- Log recorded as a file -->
22 <appender-ref = "logfileappender"/>
23 <! -- Console control display log -->
24 <! -- <Appender-ref = "leleappender"/> -->
25 <! -- Windows event log -->
26 <! -- <Appender-ref = "eventlogappender"/> -->
27 <! -- Record to database -->
28 <! -- <Appender-ref = "adonetappender_sqlserver"/> -->
29 </root>
30
31 </log4net>

There are two ways to configure the file: one is to use the built-in Web. config or app. config, the other is the custom configuration file. Here I select Custom (personal interests, no reason ).

For details about log4net logs, refer to logging.

Now, log4net is used in the project, and spring.net is used in the project.

The purpose is to use the log function in BLL.

First, add the following code in the BLL assembly.

View code

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net/log4_dal.xml",
ConfigFileExtension = "txt", Watch = true)]

Second, implement the ilog interface in the constructor.

Bll code

1 ilog log = NULL;
2 // userservice userservice1 = NULL;
3
4 Public userbll ()
5 {
6 // get the userservice instance object in the container and inject corresponding attributes
7 userservice = (iuserservice) CTX. GetObject ("exampleservice ");
8 // userservice1 = (userservice) CTX. GetObject ("userservice ");
9 log = logmanager. getlogger (typeof (userbll ));
10}

Third, use

Bll

1 /// <summary>
2 // Add a user
3 /// </Summary>
4 /// <Param name = "user"> </param>
5 public user adduser (User user)
6 {
7 string MSG = string. empty;
8 user u = userservice. adduser (User );
9
10 if (u! = NULL)
11 MSG = "User Added successfully ";
12 else
13 MSG = "failed to add user ";
14 log. Info (MSG );
15 return U;
16}

Result

A simple log is complete.

Welcome to our discussion!

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.