ASP. 5 Getting Started (3) –logging

Source: Internet
Author: User
Tags log4net

ASP. 5 Understanding and Getting Started

Build and develop an ASP 5 project

Using a custom configuration file

ASP. 5 Getting Started (3) –logging

A few days ago, I was concerned about Uncle Tom's document: Interpreting the ASP. NET 5 & MVC6 Series (9): Log frame.

It is clear that this is not the case, then a different perspective, in the implementation of the program and code development to talk about their own understanding.

Understanding the logging of ASP.NET5

The logging of ASP. 5 I think it's actually 2 words:

    • Logging use of : The Logging unified use of the Microsoft.Framework.Logging interface within the application.
    • Logging implementation : Using different proxy classes to implement technology distribution to different logging applications internally generated log.

The use of logging is actually relying on 2 basic interfaces: ILogger and Iloggerfactory.

The implementation of logging is achieved through the implementation of different Iloggerprovider proxy interface and other technology docking.

If still do not understand can look at the current Microsoft.Framework.Logging source, it is clear:

Https://github.com/aspnet/Logging/tree/dev/src

We can see that abstractions is the universal interface of logging, and console, Nlog and Tracesouce are the 3 internally provided proxy classes. Only the console and the TraceSource agent can be used at this time, And Nlog did not submit, but according to the code inside, I believe that for other logging technology such as Log4net,serilog proxy class is not difficult to implement.

How to use the Universal logging framework

There are 3 steps to using the logging framework:

    1. Use dependency injection to get the iloggerfactory implementation.
    2. Building ILogger instances with Iloggerfactory
    3. Use the ILogger instance to output the log information.

We also take the configurate function of the most classic startup as an example:

 Public voidConfigure (Iapplicationbuilder app) {//Get Iloggerfactory implementation via dependency injection using the simplest getservice function     varLoggerfactory = (iloggerfactory) app. Applicationservices.getservice (typeof(iloggerfactory)); //Creating an Logger instance     varLogger = loggerfactory. Createlogger<startup>(); //output a log messageLogger. Logwarning ("This is a warning message"); }


So the log information for these outputs is shown there, except for the VS debugging environment, it is regrettable that there is no place to see this information, because the standard logging implementation does not implement any output, if you want to output, you must join the various proxy classes to distribute the log.

Join the console Agent

First we add references:

"Microsoft.Framework.Logging.Console": "1.0.0-beta4"

Next we add the following code

var loggerfactory = (iloggerfactory) app. Applicationservices.getservice (typeof(iloggerfactory)); // Join the console agent loggerfactory. Addconsole (minLevel:LogLevel.Information);


The agent joins in the project only need to add once, generally in the Configure function joins can. After adding the above code, all log information distributed from the general interface will be displayed on the control console, although only information above or above the information level will be displayed because the MinLevel property is set.

Join TraceSource Agent

If only the console display is obviously not enjoyable, generally speaking, we will output log to the file, in the current situation we can only through the TraceSource proxy bar log output to the file. TraceSource is the main content of system.diagnostics, we should not be unfamiliar.

First, we still need to add some components

"Microsoft.Framework.Logging.TraceSource": "1.0.0-beta4"

If you want to support core 5.0, you must also join

"System.Diagnostics.TextWriterTraceListener": "4.0.0-beta-22816"

The implementation of the Code is (at development time to join the required using statement):

varLoggerfactory = (iloggerfactory) app. Applicationservices.getservice (typeof(Iloggerfactory));//Add Trace Source LoggervarLogfilestream =NewFileStream ("App.log", filemode.append);varTracelistenter =NewTextWriterTraceListener (logfilestream) {Filter=NewEventtypefilter (Sourcelevels.error |sourcelevels.critical)};varSource =NewSourceswitch ("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 current directory of the App.log file, if you want to display more levels of information, modify the filter yourself.

The agent, like the console, only needs to run once, and it is also recommended to run in Configurate.

Prospects for implementation of other agents

There are no more complete implementations of the other proxy classes, but we can foresee that the following implementation methods are not difficult:

    • First introduce the third-party log implementations you use, such as Log4net,nlog,serilog.
    • Then introduced or self-developed proxy class, generally this proxy class will implement an extension function for Iloggerfactory addxxx (): such as Addnlog, Addlog4net, Addserilog and so on.
    • Finally, call this extension function addxxx () function in the Configutate function to distribute the standard logging to the third-party log implementation.

This is the current logging distribution ideas and implementations offered by ASP. NET 5.

ASP. 5 Getting Started (3) –logging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.