Click here to go to the ABP series articles General Catalogue
DDD-based Modern ASP.--ABP Series 8, ABP Log Management
The ABP is "ASP. Boilerplate Project (ASP. NET Template project) "for short.
ABP's official website :http://www.aspnetboilerplate.com
ABP's Open source project on GitHub : https://github.com/aspnetboilerplate
This article is provided by Dongguan -Heaven translation
Server side (servers side)
The ASP. Boilerplate uses castle Windsor's logging facility logging tool, and can use different log class libraries, such as Log4net, NLog, Serilog ... Wait a minute. For all the log class libraries, Castle provides a common interface to implement, we can easily handle a variety of special log library, and when the business needs, it is easy to replace the log component.
Translator notes: What is Castle: Castle is an open source project for the. NET platform, from the data Access framework ORM to the IOC container, to the MVC framework of the web layer, to AOP, which basically includes everything in the entire development process. The IOC container for ASP. Boilerplate is implemented through castle.
Log4net is one of the most popular log library components under ASP. The ASP. NET boilerplate template also uses the Log4net Log library component, but here we are only implementing a single line of key code log4net Dependency injection (specifically described in the following configuration file), so if you want to replace your own log component, it is also easy.
Get Logger Logger
Regardless of which log library component you choose, logging through code is the same. (The Castle ' s general ILogger interface is really awesome).
Belowto get to the point: (Translator Note: The following code is the ABP framework of Castle.core source analysis and implementation)
1, first of all, we have to deal with the Logger object logger, the ASP. NET Boilerplate framework uses dependency injection Dependency injection technology, we can easily use dependency injection to generate the Logger object logger.
Let's take a look at how ASP. NET boilerplate is implemented with logging capabilities:
usingCastle.Core.Logging;//1: Import the log namespace, Castle.Core.Logging
Public classtaskappservice:itaskappservice{ //2: Gets the Logger object through dependency injection. This first defines the public property of the ILogger type logger, which is the object that we use to record the log. After creating the Taskappservice object (which is the task defined in our application), it is implemented through the method of attribute injection. PublicILogger Logger {Get;Set; } PublicTaskappservice () { //3: If there is no logger, return the logger to an empty instance without writing the log. This is the best way to implement dependency injection,
If you do not define this empty logger, an exception is generated when we get the object reference and instantiate it.
Doing so ensures that the object is not empty. So, in other words, without setting up the logger, the log is not logged and a null object is returned.
Nulllogger objects actually all wood has, empty. In doing so, we can ensure that our defined classes function properly when instantiated. Logger =nulllogger.instance; }
Public voidcreatetask (createtaskinput input){ //4: Write LogLogger.info ("Creating A new task with description:"+input. Description);
//todo:save task to database ...
} }
After writing the log, we can view the log file, just like the following format:
INFO 2014-07-13 13:40:23,360 [8] simpletasksystem.tasks.taskappservice-creating a new task with Description:remember to Drink milk before sleeping!
Using Logger with base classes
The ASP. NET Boilerplate framework provides the base classes for MVC Controllers, Web API Controllers, and Application service classes (their own defined controllers and application services, Must inherit the base class of the ASP. Boilerplate, in other words, when your custom Web API controllers, MVC controllers,application service classes inherit the ASP. Boilerplate framework corresponding to the base class, you can directly use the logger).
Public class Homecontroller:simpletasksystemcontrollerbase { public actionresult Index () { logger.debug ("A sample Log message ... " ); return View ();
Description: Simpletasksystemcontrollerbase This base class controller is our own defined base class controller, and he must inherit from Abpcontroller.
This enables the logger to work correctly. Of course, you can also implement your own base class, so that you can also not use dependency injection.
Configuration
If you build your project on the official website via ASP. Boilerplate templates, all of the log4net configuration is automatically generated.
The default configuration format is as follows:
- Log level: Logging levels, with Debug, INFO, WARN, ERROR or FATAL5.
- Dateand time: Logging times.
- Thread Number: The code for each line when the log is written.
- LoggerName: The name of the logger, usually the name of the class.
- Log text: The content of the logs you write.
Profiles: Log4net.config are typically under the project's Web directory.
<?XML version= "1.0" encoding= "Utf-8"?><log4net> <Appendername= "Rollingfileappender"type= "log4net." Appender.rollingfileappender " > <filevalue= "Logs/logs.txt" /> <Appendtofilevalue= "true" /> <Rollingstylevalue= "Size" /> <maxsizerollbackupsvalue= "Ten" /> <maximumFileSizevalue= "10000KB" /> <Staticlogfilenamevalue= "true" /> <Layouttype= "log4net." Layout.patternlayout "> <Conversionpatternvalue= "%-5level%date [%-5.5thread]%-40.40logger-%message%newline" /> </Layout> </Appender> <Root> <Appender-refref= "Rollingfileappender" /> < Levelvalue= "DEBUG" /> </Root> <Loggername= "NHibernate"> < Levelvalue= "WARN" /> </Logger></log4net>
Log4net is a very powerful and easy to use log library component, you can write various logs, such as write to TXT file, write to database and so on. You can set the minimum log level, like this one for the NHibernate configuration. Different loggers write different logs, and so on.
For specific usage, you can refer to: http://logging.apache.org/log4net/release/config-examples.html
Finally, in the project's Global.asax file, define the configuration file for the log4net:
Public class mvcapplication:abpwebapplication{ protectedoverridevoid application_ Start (object sender, EventArgs e) { IocManager.Instance.IocContainer.AddFacility< Loggingfacility> (f = f.uselog4net (). Withconfig ("log4net.config")); Base . Application_Start (sender, E);} }
A few lines of code call the Log4net log component, the Log4net library in the project is in the NuGet package, and you can switch to another Log component library, but the code doesn't have to change anything. Because, our framework is to implement the logger through dependency injection!
Client side (clients)
Finally, what's more, you can also invoke the logger on the client. At the client, the ASP. NET boilerplate framework has the corresponding JavaScript log API, which means you can log down the browser's logs, implementing the code as follows:
Attached: The client-side JavaScript API, here to illustrate is that you can use Console.log in the client output log, but this API does not necessarily support all browsers, and may cause your script to appear abnormal, you can use our API, our is safe, You can even overload or extend these APIs.
Abp.log.debug (' ... ');abp.log.info(' ... ');abp.log.warn(' ... ');abp.log.error (' ... ') ); Abp.log.fatal (' ... ');
I hope that more domestic architects will be able to focus on the ABP project, and perhaps it will help you, perhaps with your participation, this project can develop better.
Welcome to add ABP Architecture Design Exchange QQ Group: 134710707
Click here to go to the ABP series articles General Catalogue
DDD-based Modern ASP.--ABP Series 8, ABP Log Management