Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of logging Framework in ASP.NET5 and MVC6

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of the log framework in ASP.NET5 and MVC6. I hope you will get something after reading this article. Let's discuss it together.

Framework introduction

ILoggerFactory interface

API ILoggerFactory is the entry point of logs. You can obtain an instance of this API through dependency injection in the system, and create a logger ILogger to record logs according to this example. Examples are as follows:

Var factory = ServiceProvider.GetRequiredService (); var logger1 = factory.CreateLogger (typeof (HomeController) .FullName); / / CreateLogger var logger2 = factory.CreateLogger (); / / CreateLogger logger1.Log (LogLevel.Information, 1, null, null, null); / / logging logger1.LogInformation ("123"); / / extension method logger1.LogError (" 123"); / / extension method

Alternatively, you can get the above example from the loggerfactory parameter in the Configure method of Startup.cs.

The definition of ILoggerFactory interface is as follows:

Public interface ILoggerFactory {/ / minimum logging level LogLevel MinimumLevel {get; set;} / / create logging instance ILogger CreateLogger (string categoryName); / / generally classify void AddProvider (ILoggerProvider provider) according to functional module or class name; / / add logging provider (such as third-party implementation)}

In the implementation of this interface, we can set the basic minimum record of the log, which can be classified as follows:

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 a console version:

Public static ILoggerFactory AddConsole (this ILoggerFactory factory) {factory.AddProvider (new ConsoleLoggerProvider ((category, logLevel) = > logLevel > = LogLevel.Information); return factory;}

Then create a logger instance through the CreateLogger method, and finally record the log.

ILoggerProvider and ILogger

All third-party implementations need to implement ILoggerProvider interface and ILogger interface. The interface is very simple, just implement the method of creating ILogger interface. The code is as follows:

Public interface ILoggerProvider {ILogger CreateLogger (string name); / / create an ILgger instance of a given category}

The implementation of ILogger is also relatively simple. In addition to implementing a general logging method, you also need to implement a log level judgment method and a scope creation method. The API is defined as follows:

Public interface ILogger {/ / supports most common logging methods, while other access improves void Log (LogLevel logLevel, int eventId, object state, Exception exception, Func formatter) through extended methods; / / determines whether a given log level bool IsEnabled (LogLevel logLevel) can be recorded; / / opens a logical operation scope IDisposable BeginScopeImpl (object state);}

Currently, four logging Provider are implemented by default in ASP.NET 5, namely: Console, NLog, Serilog and Trace. When registering these Provider, you can use the extension method. An example is as follows:

Loggerfactory.AddConsole () loggerfactory.AddNLog (new NLog.LogFactory ()) loggerfactory.AddSerilog (new LoggerConfiguration ()) var testSwitch = new SourceSwitch ("TestSwitch", "Level will be set to warning for this test"); factory.AddTraceSource (testSwitch, new ConsoleTraceListener ())

The extension method of ILogger

In order to facilitate logging, Microsoft has defined six extension methods for six levels of logging on Microsoft.Framework.Logging.LoggerExtensions as follows:

Public static void LogInformation (this ILogger logger, string message) public static void LogInformation (this ILogger logger, int eventId, string message) public static void LogInformation (this ILogger logger, string format, params object [] 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 using it, we can use methods like LogDebug () and LogError () to log quickly. In addition, this class has three levels: Warning, Error and Critical, and two extension methods are defined respectively. The example is as follows:

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 expansion methods are estimated to be invincible all over the world.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report