ASP. NET 5 (3)-Logging, asp. netlogging
Introduction to ASP. NET 5
Create and Develop ASP. NET 5 projects
Use a custom configuration file
ASP. NET 5 (3)-Logging
I followed Uncle Tom's related documents a few days ago: Interpreting ASP. NET 5 & MVC6 series (9): log framework.
I have already made it clear that I will not talk about it here. From another perspective, let's talk about our own understanding in terms of implementation solutions and code development.
Understanding ASP. NET5 Logging
I think ASP. NET 5's Logging is actually two sentences:
- LoggingUse: Unified use of Logging in applicationsMicrosoft. Framework. LoggingInterface.
- LoggingImplementation: Different Agent classes are used to distribute logs generated within the application to different Logging technologies.
Logging relies on two basic interfaces: ILogger and ILoggerFactory.
Logging is implemented by implementing different ILoggerProvider proxy interfaces to connect with other technologies.
If you still do not understand the source code of Microsoft. Framework. Logging, you will be clear:
Https://github.com/aspnet/Logging/tree/dev/src
As we can see, veriactions is the general interface of Logging, and Console, Nlog and TraceSouce are the three internal proxies. currently, only the Console and TraceSource proxies can be used, but NLog is not submitted. However, based on the code, I believe that the Serilog proxy classes are not difficult to implement for other Logging technologies such as Log4Net.
How to use the General Logging framework
There are three steps to use the Logging framework:
Let's take the most classic Startup aggregate ate function as an example:
Public void Configure (IApplicationBuilder app) {// use the simplest GetService function to get ILoggerFactory through dependency injection to implement var loggerfactory = (ILoggerFactory) app. applicationServices. getService (typeof (ILoggerFactory); // create a logger instance var logger = loggerfactory. createLogger <Startup> (); // output a Log information logger. logWarning ("this is a warning message ");}
So the output Log information is displayed there. Except for the VS debugging environment, it is a pity that nothing can be seen here, because the standard Logging implementation does not implement any output, to output logs, you must add various proxy classes to distribute logs.
Join Console proxy
First, we add reference:
"Microsoft. Framework. Logging. Console": "1.0.0-beta4"
Next, add the following code:
Var loggerfactory = (ILoggerFactory) app. ApplicationServices. GetService (typeof (ILoggerFactory); // Add loggerfactory. AddConsole (minLevel: LogLevel. Information) as the Console proxy );
The proxy only needs to be added once in the project. Generally, it is added to the Configure function. after the above Code is added, all Log Information distributed from the common interface will be displayed on the program console. Of course, because the minLevel attribute is set, only Information above or equal to the Information level will be displayed.
Add TraceSource proxy
If the log is displayed only on the console, it is obviously not satisfactory. In general, we will output the Log to the file. In the current situation, we can only output the log to the file through the TraceSource proxy. traceSource is System. the main content in Diagnostics should be familiar to everyone.
First, we need to add some components.
"Microsoft. Framework. Logging. TraceSource": "1.0.0-beta4"
To support Core 5.0, you must also add
"System. Diagnostics. TextWriterTraceListener": "4.0.0-beta-22816"
The implementation code is (add the required using statement during development ):
var loggerfactory = (ILoggerFactory)app.ApplicationServices.GetService(typeof(ILoggerFactory));// Add trace source loggervar logFileStream = new FileStream("app.log", FileMode.Append);var traceListenter = new TextWriterTraceListener(logFileStream){ Filter = new EventTypeFilter(SourceLevels.Error | SourceLevels.Critical)};var source = new SourceSwitch("AppLog"){ Level = SourceLevels.All};loggerfactory.AddTraceSource(source, traceListenter);
According to the above Code, all the Error and Critical Log information will be output to the app. log file in the current directory. to display more levels of information, Please modify the Filter.
The proxy is the same as the Console and only needs to be run once. We also recommend that you run the proxy in the replicate.
Other proxies
Other proxy classes are not fully implemented at present, but we can foresee that the subsequent implementation method is not difficult, that is:
- First, introduce the third-party Log implementation you are using, such as Log4Net, NLog, and Serilog.
- Then introduce or develop the proxy class. Generally, this proxy class implements an extension function AddXXX () for ILoggerFactory, such as AddNlog, AddLog4Net, and AddSeriLog.
- Finally, the extension function AddXXX () is called in the Configutate function to distribute the standard Logging to the third-party Log implementation.
This is the current Logging Distribution Idea and implementation solution provided by ASP. NET 5.