Framework Introduction
In the previous. NET, Microsoft has not provided a decent log frame, currently can use some of the framework such as Log4net, Nlog, commonlogging used up more or less difficult, and Java slf4j can not compare. But in the new version of the ASP.NET5, can be described as bullish, Microsoft provides Microsoft.Framework.Logging framework is the slf4j of the. NET version, providing the appropriate interface, other Third-party components in accordance with the interface to achieve their own implementation.
Iloggerfactory interface
ILoggerFactory
An interface is the storage point of a log in which an instance of the interface can be obtained by dependency injection, and a logger is created from the example to ILogger
record the log, as shown in the following example:
var factory = serviceprovider.getrequiredservice<iloggerfactory> ();
var logger1 = factory. Createlogger (typeof (HomeController). FullName); Createlogger
var logger2 = factory. Createlogger
Alternatively, you can get the above example from the Loggerfactory parameter in the Startup.cs configure method.
ILoggerFactory
The interface is defined as follows:
Public interface iloggerfactory
{
//log minimum record level
loglevel minimumlevel {get; set;}
Create a log record instance
ILogger Createlogger (string CategoryName);//typically categorize void addprovider based on a functional module or class name
(Iloggerprovider Provider); Add logging provider (for example, third party implementations)
}
In the implementation of this interface, we can set the minimum logging base for the log, which is the following categories:
Public enum loglevel
{
Debug = 1,
Verbose = 2,
information = 3,
Warning = 4,
Error = 5,
Critical = 6,
}
You can also add a third-party implementation of provider, such as adding a console version of the implementation:
public static Iloggerfactory Addconsole (this iloggerfactory factory)
{
Factory. AddProvider (New Consoleloggerprovider (category, LogLevel) => loglevel (>= loglevel.information));
return factory;
}
The logger instance is then created by the Createlogger method, and the log is finally logged.
Iloggerprovider and ILogger
All Third-party implementations need to implement the Iloggerprovider interface and the ILogger interface, where the interface is very simple, that is, to implement the method of creating the ILogger interface, the code is as follows:
Public interface Iloggerprovider
{
ilogger createlogger (string name);//Create a Ilgger instance of a given category
}
and the implementation of ILogger is relatively simple, in addition to the implementation of common logging methods, but also need to implement a log-level judgment method and a scope creation method, interface definition is as follows:
Public interface ILogger
{
//supports common methods for most logging, and other accesses are perfected by extension methods
void Log (loglevel loglevel, int eventId, Object state, Exception Exception, Func<object, Exception, string> formatter);
Determines whether the given log level can be logged
isenabled (loglevel loglevel);
Opens a logical operation scope
IDisposable Beginscopeimpl (object state);
Implementation of the above two interfaces, the factory
AddProvider
method can be passed, the provider added to the instance, to achieve the purpose of logging. The current default implementation in ASP.net 5 is the provider of logging in 4, respectively: Console, Nlog, Serilog, Trace, when registering these provider, you can use the extension method, examples are as follows:
Loggerfactory. Addconsole ()
loggerfactory. Addnlog (New Nlog.logfactory ())
loggerfactory. Addserilog (New Loggerconfiguration ())
var testswitch = new Sourceswitch ("Testswitch", "Level'll be" is set to warning fo R this Test ");
Factory. Addtracesource (Testswitch, New Consoletracelistener ());
The extension method of ILogger
To facilitate logging, Microsoft has Microsoft.Framework.Logging.LoggerExtensions
defined 6 extension methods for 6 levels of logging, as follows:
The public static void Loginformation (this ilogger logger, string message) is public
static void Loginformation (this ILogger logger, int eventId, string message) public
static void Loginformation (this ilogger logger, string format, params obj ect[] args public
static void Loginformation (this ilogger logger, int eventId, string format, params object[] args)
public static void Loginformation (this ilogger logger, ilogvalues state, Exception error = NULL) public
static void Loginformation (This ILogger logger, int. eventId, ilogvalues state, Exception error = NULL)
/Other debug, Verbose, Warning , Error, critical also follow the logxxxx () rule.
So when used, we can use methods like Logdebug (), Logerror () to log quickly. In addition, the class also has three levels for warning, Error, critical, and 2 extension methods respectively, as shown in the following example:
public static void Logwarning (this ilogger logger, string message, Exception error) public
static void Logwarning (this ILogger logger, int eventId, string message, Exception error)
Some of these extension methods are used to estimate the world without rivals.
Summarize
Through the interface based programming mechanism and DI Dependency injection mechanism, we can easily implement the extension of Third-party log provider, so as to record the log to any place we want to record, such as MongoDB and other NoSQL databases.