Windsor Introductory Tutorial---Part V add log function

Source: Internet
Author: User
Tags log4net

Introduced

Now that we have the basic framework, it is time to add the content, so the first thing we should consider is to add the log function to the application. We will use Windsor to configure, in this section, you will learn other functions outside of Windsor.

Logging Facility

As mentioned in the previous section, Windsor has a number of optional functional components that are available, and they extend the usability of Windsor. In this section, we will add the log function to the program.

Logging facility provides a number of common log frameworks such as Log4net, Nlog, and MVC built-in log framework trace. Logging facility will provide an abstract class that will not be cumbersome when your program switches other log frames. More importantly, facility provides ILogger instances as needed, and does not require any static dependencies.

We need to add the necessary packages, start NuGet's package Manager console and type:

Install-package castle.windsor-log4net

This will add dependencies to the program:

Installer

Next add the assembly reference, create a installer to add the log function into the program:

Note the invocation pattern of the API, the generic parameter specifies the type that we want to add, and then use the lambda expression to make the type log4net.

We did not specify a configuration file for log4net, the default configuration is in Log4net. Config file, which is the core file of the log4net configuration. We add a configuration file to the program that contains the standard log4net configuration information:

Then modify the Installer class:

What do we just did?

You may ask: How is this used? All you have to do is invoke the ILogger interface under the Castle.Core.Logging.Ilogger namespace (the common practice is to use attributes rather than constructors). The container will provide the configuration and prepare to use the ILogger instance. To illustrate that we have added a warning log to the AccountController log when the user logs in a password error:

If the configuration of the log4net is correct, the log that is now logged in error will be logged.

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.