ABP框架系列之三十二:(Logging-登录)
Server Side(服务端)
ASP.NET Boilerplate uses Castle Windsor's logging facility. It can work with different logging libraries: Log4Net, NLog, Serilog... etc. Castle provides a common interface for all logger libraries. So, you're independent from specific logging library and can easily change it later if you need.
ASP.NET模板使用Castle Windsor的日志库。它可以与不同日志库工作:log4net,NLog,Serilog…Castle为所有的日志库提供了一个公共接口。因此,您可以独立于特定的日志库,如果需要,可以稍后更改它。
Log4Net is one of the most popular logging libraries for .NET. ASP.NET Boilerplate templates come with Log4Net properly configured and working. But, there is just a single-line of dependency to log4net (as seen inconfiguration section), so you can change it to your favourite library.
log4net是最受欢迎的日志库。ASP.NET标准模板来log4net正确配置和工作。但是,只有一行依赖log4net(看结构部分),所以你可以改为你最喜欢的库。
Getting The Logger
No matter which logging library you choice, the code to write log is same (thanks to Castle's common ILogger interface).
无论你选择的日志库,代码写的日志是一样的(感谢ILogger Castle's的通用接口)。
First, we should get a Logger object to write logs. Since ASP.NET Boilerplate strongly uses dependency injection, we can easily inject a Logger object using property injection (or constructor injection) pattern. See a sample class that writes a log line:
首先,我们应该得到一个日志记录器对象来编写日志。自从ASP.NET样板采用依赖注入,我们可以很容易地将使用属性注入记录器对象(或构造函数注入)模式。参见一个编写日志行的示例类:
using Castle.Core.Logging; //1: Import Logging namespace public class TaskAppService : ITaskAppService
{
//2: Getting a logger using property injection
public ILogger Logger { get; set; } public TaskAppService()
{
//3: Do not write logs if no Logger supplied.
Logger = NullLogger.Instance;
} public void CreateTask(CreateTaskInput input)
{
//4: Write logs
Logger.Info("Creating a new task with description: " + input.Description); //TODO: save task to database...
}
}
First, we imported namespace of Castle's ILogger interface.
首先,我们导入的Castle的ILogger接口命名空间。
Second, we defined a public ILogger object named Logger. This is the object we will write logs. Dependency injection system will set (inject) this property after creating TaskAppService object. This is known as property injection pattern.
其次,我们定义了一个公共ILogger对象命名为记录器。这是我们将要写入日志的对象。依赖注入(注入)系统将设置此属性在创建taskappservice对象。这就是属性注入模式。
Third, we set Logger to NullLogger.Instance. System will work fine without this line. But this is best practice on property injection pattern. If no one sets the Logger, it will be null and we get an "object reference..." exception when we want to use it. This guaranties that it's not null. So, if no one sets the Logger, it will be NullLogger. This is known as null object pattern. NullLogger actually does nothing, does not write any logs. Thus, our class can work with and without an actual logger.
第三,我们要nulllogger.instance记录器。没有这条线,系统会运转良好。但这是属性注入模式的最佳实践。如果没有人设置记录器,它将是空的,当我们想使用它时,会得到一个“对象引用”…异常。这保证了它不是空的。所以,如果没有一套记录器,它将NullLogger。这就是所谓的空对象模式。nulllogger其实什么都不做,不写任何日志。因此,我们的类可以使用和不使用实际的记录器。
Fourth and the last, we're writing a log text with info level. There are different levels (see configuration section).
第四和最后一个,我们正在用信息级别写一个日志文本。有不同的级别(参见配置部分)。
If we call the CreateTask method and check the log file, we see a log line as like as shown below:
INFO 2014-07-13 13:40:23,360 [8 ] SimpleTaskSystem.Tasks.TaskAppService - Creating a new task with description: Remember to drink milk before sleeping!
Base Classes With Logger
ASP.NET Boilerplate provides base classes for MVC controllers, Web API controllers, Application service classes and more. They declare a Logger property. So, you can directly use this Logger to write logs, no injection needed. Example:
ASP.NET样板提供了MVC的控制器基类,Web API控制器,应用服务类和更多。它们声明记录器属性。因此,您可以直接使用这个日志记录器来编写日志,不需要注入。例子:
public class HomeController : SimpleTaskSystemControllerBase
{
public ActionResult Index()
{
Logger.Debug("A sample log message...");
return View();
}
}
Note that SimpleTaskSystemControllerBase is our application specific base controller that inherits AbpController. Thus, it can directly use Logger. You can also write your own common base class for other classes. Then, you will not have to inject logger every time.
注意,simpletasksystemcontrollerbase是我们的基础,inherits abpcontroller应用的专用控制器。因此,它可以直接使用记录器。你也可以写自己的共同基础级给其他类。然后,你将不必每次注射记录器。
Configuration
All configuration is done for Log4Net when you create your application from ASP.NET Boilerplate templates.
所有配置好了log4net当你创建你的应用从ASP.NET标准模板。
Default configuration's log format is as show below (for each line):
- Log level: DEBUG, INFO, WARN, ERROR or FATAL.
- Date and time: The time when the log line is written.
- Thread number: The thread number that writes the log line.
- Logger name: This is generally the class name which writes the log line.
- Log text: Actual log text that you write.
日志级别:调试、信息、警告、错误或致命。
日期和时间:记录日志行的时间。
线程号:写入日志行的线程号。
记录器名称:这通常是写入日志行的类名。
日志文本:您编写的实际日志文本。
It's defined in the log4net.config file of the application as shown below:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="Logs/Logs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
<level value="DEBUG" />
</root>
<logger name="NHibernate">
<level value="WARN" />
</logger>
</log4net>
Log4Net is highly configurable and strong logging library. You can write logs in different formats and to different targets (text file, database...). You can set minimum log levels (as set for NHibernate in this configuration). You can write diffent loggers to different log files. It can automatically backup and create new log file when it reaches to a specific size (Rolling file adapter with 10000 KB per file in this configuration) and so on... Read it's own confuguration documentation for more.
log4net是高度可配置的,强大的日志库。你可以用不同的格式和不同的目标(文本文件、数据库……)编写日志。你可以设置最小日志级别(如在这个配置NHibernate)。你可以写不同的伐木工人到不同的日志文件。它可以自动备份和创建新的日志文件,当它达到一个特定的大小(滚动文件适配器10000 KB每文件在这个配置)等…阅读更多它自己的配置文件。
Finally, in the Global.asax file, we declare that we use Log4Net with log4net.config file:
public class MvcApplication : AbpWebApplication
{
protected override void Application_Start(object sender, EventArgs e)
{
IocManager.Instance.IocContainer.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("log4net.config"));
base.Application_Start(sender, e);
}
}
This is the only code line we directly depend on log4net. Also, only the web project depends on log4net library's nuget package. So, you can easily change to another library without changing your logging code.
这是唯一的代码行我们直接取决于log4net。同时,只有Web项目取决于log4net库的NuGet包。因此,在不改变日志代码的情况下,您可以轻松地更改到另一个库。
Abp.Castle.Log4Net Package
ABP uses Castle Logging Facility for logging and it does not directly depend on log4net, as declared above. But there is a problem with Castle's Log4Net integration. It does not support the latest log4net. We created a nuget package, Abp.Castle.Log4Net, to solve this issue. After adding this package to our solution, all we should do is to change the code in application start like that:
ABP采用 Castle并不直接取决于log4net,作为以上的声明。但有 Castle log4net整合问题。它不支持最新log4net。我们创建了一个NuGet包,abp.castle.log4net,解决这个问题。将这个包添加到我们的解决方案之后,我们所要做的就是改变应用程序中的代码:
public class MvcApplication : AbpWebApplication
{
protected override void Application_Start(object sender, EventArgs e)
{
IocManager.Instance.IocContainer.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net().WithConfig("log4net.config"));
base.Application_Start(sender, e);
}
}
The only difference is that we used "UseAbpLog4Net()" method (defined in Abp.Castle.Logging.Log4Net namespace) instead of "UseLog4Net()". When we use Abp.Castle.Log4Net package, it's not needed to useCastle.Windsor-log4net and Castle.Core-log4net packages.
Client Side
ASP.NET Boilerplate defines a simple javascript logging API for client side. It logs to browser's console as default. Sample javascript code to write logs:
abp.log.warn('a sample log message...');
For more information, see logging API documentation.
ABP框架系列之三十二:(Logging-登录)的更多相关文章
- ABP框架系列之三十四:(Multi-Tenancy-多租户)
What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...
- ABP框架系列之十二:(Audit-Logging-审计日志)
Introduction Wikipedia: "An audit trail (also called audit log) is a security-relevant chronolo ...
- ABP框架系列之三十九:(NLayer-Architecture-多层架构)
Introduction Layering of an application's codebase is a widely accepted technique to help reduce com ...
- ABP框架系列之三十五:(MVC-Controllers-MVC控制器)
Introduction ASP.NET Boilerplate is integrated to ASP.NET MVC Controllers via Abp.Web.Mvc nuget pack ...
- ABP框架系列之三十八:(NHibernate-Integration-NHibernate-集成)
ASP.NET Boilerplate can work with any O/RM framework. It has built-in integration with NHibernate. T ...
- ABP框架系列之四十二:(Object-To-Object-Mapping-对象映射)
Introduction It's a common to map a similar object to another object. It's also tedious and repeatin ...
- ABP框架系列之五十二:(Validating-Data-Transfer-Objects-验证数据传输对象)
Introduction to validation Inputs of an application should be validated first. This input can be sen ...
- ABP框架系列之三十六:(MVC-Views-MVC视图)
Introduction ASP.NET Boilerplate is integrated to MVC Views via Abp.Web.Mvc nuget package. You can c ...
- ABP框架系列之三十:(Javascript-API-Javascript-API)
ASP.NET Boilerplate provides a set of objects and functions that are used to make javascript develop ...
随机推荐
- 涂抹mysql笔记-数据导出导入
数据导出导入<>利用CSV存储引擎加载数据:CSV存储引擎基于CSV格式文件存储数据,CSV格式是纯文本格式的文件,以逗号分隔取值.CSV引擎表的所有列值不能为空.Excel可以直接打开有 ...
- 第23课 可变参数模板(4)_Optional和Lazy类的实现
1. optional类的实现 (1)optional的功能 ①optional<T>的内部存储空间可能存储了T类型的值,也可能没有.只有当optional被T初始化之后,这个option ...
- Linux背背背(3)
目录 1.文件操作命令 2.文件夹操作命令 文件操作命令 创建 命令:touch 语法:#touch 文件的名字 文件名可以是一个完整的路径 如果后面的参数文件名指定了路径,则表示在指定的路 ...
- pyton unittest
在说unittest之前,先说几个概念: TestCase 也就是测试用例 TestSuite 多个测试用例集合在一起,就是TestSuite TestLoader是用来加载TestCase到Test ...
- python中建模分析零息票收益率曲线--复利和连续复利
收益率曲线(Yield Curve)是显示一组货币和信贷风险均相同,但期限不同的债券或其他金融工具收益率的图表.纵轴代表收益率,横轴则是距离到期的时间.在此用python建模分析零息票收益率曲线,输出 ...
- py库:文本转为语音(pywin32、pyttsx)
http://blog.csdn.net/marksinoberg/article/details/52137547 Python 文本转语音 文本转为语音(使用Speech API) 需要安装 py ...
- 两个时间点计算相隔几年,几个月,几天-java
本文采用Calendar 实现 ,当然也可以用java8提供的愉快且方便的时间处理- LocalDate import java.text.ParseException; import java.te ...
- Python3执行DOS命令并截取其输出到一个列表字符串,同时写入一个文件
#执行DOS命令并截取其输出到一个列表字符串,同时写入一个文件#这个功能很有用listing=os.popen('ipconfig').readlines()for i in listing: pri ...
- leetcode31
class Solution { public: void nextPermutation(vector<int>&nums) { int len = nums.size(); , ...
- 深度学习原理与框架-Tensorflow卷积神经网络-cifar10图片分类(代码) 1.tf.nn.lrn(局部响应归一化操作) 2.random.sample(在列表中随机选值) 3.tf.one_hot(对标签进行one_hot编码)
1.tf.nn.lrn(pool_h1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75) # 局部响应归一化,使用相同位置的前后的filter进行响应归一化操作 参数 ...