Framework Introduction In the previous .NET, Microsoft has not provided a decent logging framework. Some of the currently available frameworks, such as Log4Net, NLog, and CommonLogging, are more or less difficult to use and cannot be compared with Java's SLF4J. However, in the new version of ASP.NET 5, it is very powerful. The Microsoft.Framework.Logging framework set provided by Microsoft is the .NET version of SLF4J, providing corresponding interfaces, and other third-party components can implement their own implementations based on the interfaces. ILoggerFactory Interface The ILoggerFactory interface is the entry point for logs. You can get an instance of this interface through dependency injection in the system, and create a logger ILogger to record logs based on this example. The example is as follows: var factory = ServiceProvider.GetRequiredService Alternatively, you can also get the above example from the loggerfactory parameter in the Configure method of Startup.cs. The definition of the ILoggerFactory interface is as follows:
In the implementation of this interface, we can set the minimum record base of the log, the categories are as follows
You can also add a third-party provider, such as a console version:
Then create a logger instance through the CreateLogger method, and then record the log. ILoggerProvider and ILogger All third-party implementations need to implement the ILoggerProvider interface and the ILogger interface. The interface is very simple, which is to implement the method of creating the ILogger interface. The code is as follows:
The implementation of ILogger is relatively simple. In addition to implementing the general logging method, it also needs to implement a log level judgment method and a scope creation method. The interface definition is as follows:
After implementing the above two interfaces, you can add the provider to the instance through the factory's AddProvider method to achieve the purpose of logging. Currently, ASP.NET 5 implements four logging providers by default, namely: Console, NLog, Serilog, and Trace. When registering these providers, you can use extension methods. The examples are as follows:
Extension methods of ILogger To facilitate logging, Microsoft has defined six extension methods for each of the six levels of logging in Microsoft.Framework.Logging.LoggerExtensions. The examples are as follows:
So when using it, we can use methods like LogDebug() and LogError() to quickly record logs. In addition, this class also defines two extension methods for the three levels of Warning, Error, and Critical, as shown below:
Some of these extension methods are probably invincible when used. Summarize Through the interface-based programming mechanism and DI dependency injection mechanism, we can easily implement the extension of the third-party log provider, so as to record the logs to any place we want, such as NoSQL databases such as MongoDB. |
<<: Interpreting ASP.NET 5 & MVC6 Series (8): Session and Caching
>>: Interpreting ASP.NET 5 & MVC6 Series (10): Controller and Action
1. Tribute to Mr. Ye Maozhong There is one thing ...
[[149898]] Summer has passed, but O2O has not yet...
Complete set of skipping rope teaching videos - C...
At the Geek Park Innovation Conference two days a...
Processing emotions, especially distinguishing da...
After the Chinese Valentine's Day, we will us...
Xiaomi has struck a very delicate balance between...
How to optimize gaming apps and how to promote th...
On November 17, one of the new features Apple int...
[[127236]] The Investment and M&A Department ...
Speaking of square dancing, I believe everyone is ...
ChinaJoy is highly anticipated, and Touch Educati...
AEB (automatic emergency braking system) technolo...
In the past few days, a "performance art&quo...
When 2b products are being sold and acquiring cus...