[Asp.net 5] Logging-basic architecture of the log system (below), asp. netlogging-

Source: Internet
Author: User

[Asp.net 5] Logging-basic architecture of the log system (below), asp. netlogging-

Next, we will continue to explain other parts of the log.

ILoggerProvider and extension class

The architecture diagram in the previous section does not show any implementation classes that directly implement this interface. So what will happen if you use the Logger class directly?

var factory = new LoggerFactory();var logger = factory.CreateLogger("name");logger.Log(LogLevel.Debug, 0, "state", null, null);

This code runs normally but does not record any logs. Because the Logger class does not directly record logs, you need to use the GetProviders method of LoggerFactory to create ILogger-type logs that can work directly. So even if there are no extensions for ILoggerProvider in the current project, we can predict that: Microsoft. framework. logging. console, Microsoft. framework. logging. NLog, Microsoft. framework. logging. traceSource implements ILoggerProvider. Of course, because ILoggerProvider produces ILogger, they also implement the ILogger class.
ILogValues and extensions

We can see that the Log logging interface is: Log (LogLevel logLevel, int eventId,Object state,Exception exception,Func <object, Exception, string> formatter)

This interface combines state and exception into a string object through formatter, and then records the object to log. If you use the extension method, The formatter passed in by the system will always be: MessageFormatter

Private static string MessageFormatter (object state, Exception error) {if (state = null & error = null) {throw new InvalidOperationException ("No message or exception details were found" + "to create a message for the log. ");} if (state = null) {return error. toString () ;}if (error = null) {return state. toString ();} return string. format (CultureInfo. currentCulture, "{0} {1} {2}", state, Environment. newLine, error );}MessageFormatter

If the information (state) to be recorded is complex and is one or more complex objects, we generally useILogValuesObject.

The system provides two ILogValues interfaces: FormattedLogValues and ReflectionBasedLogValues.

  • FormattedLogValues)
  • ReflectionBasedLogValues: obtains all attributes of a class through reflection. (This class will be used in extended log)
Public class FormattedLogValues: ILogValues {private static ConcurrentDictionary <string, LogValuesFormatter> _ formatters = new ConcurrentDictionary <string, LogValuesFormatter> (); private readonly LogValuesFormatter _ formatter; private readonly object [] _ values; public FormattedLogValues (string format, params object [] values) {_ formatter = _ formatters. getOrAdd (format, f => new LogValuesFormatter (f); _ values = values;} public IEnumerable <KeyValuePair <string, object> GetValues () {return _ formatter. getValues (_ values);} public override string ToString () {return _ formatter. format (_ values );}}FormattedLogValues public class ReflectionBasedLogValues: ILogValues {public virtual IEnumerable <KeyValuePair <string, object> GetValues () {var values = new List <KeyValuePair <string, object> (); var properties = GetType (). getTypeInfo (). declaredProperties; foreach (var propertyInfo in properties) {values. add (new KeyValuePair <string, object> (propertyInfo. name, propertyInfo. getValue (this);} return values ;}}ReflectionBasedLogValues

Other classes

  • LogLevel: Log Level
  • LogFormatter: similar to MessageFormatter, it provides different formmater methods.

Related Article

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.