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.