1024programmer Asp.Net Analysis on the use of excellent log framework under .net core, with source code attached

Analysis on the use of excellent log framework under .net core, with source code attached

Analysis on the use of excellent log framework under .net core, source code attached

In .NET Core, logging is a very important component, which can help us record the running status of the application so that we can troubleshoot when problems occur. In this article, we will introduce five excellent .NET Core logging frameworks, namely Serilog, NLog, Log4Net, Microsoft.Extensions.Logging, and Loupe. We will provide usage methods and steps for each framework, and provide source code examples.

1. Serilog

Serilog is a highly extensible .NET Core logging framework that supports multiple output formats, including console, file, Elasticsearch, and more. Serilog is characterized by strong configurability, supports chain calls, and can customize log format and output methods.

How to use

  1. Install Serilog NuGet package:
Install-Package Serilog
 Install-Package Serilog.Sinks.Console
  1. Configure Serilog at the program entry:
using Serilog;

 public static void Main(string[] args)
 {
     Log.Logger = new LoggerConfiguration()
         .MinimumLevel.Debug()
         .WriteTo.Console()
         .CreateLogger();

     // ...
 }
  1. Log:
Log.Information("This is an information message.");
 Log.Warning("This is a warning message.");
 Log.Error("This is an error message.");

Sample code

using Serilog;

 public class Program
 {
     public static void Main(string[] args)
     {
         Log.Logger = new LoggerConfiguration()
             .MinimumLevel.Debug()
             .WriteTo.Console()
             .CreateLogger();

         Log.Information("Hello, Serilog!");

         Log.CloseAndFlush();
     }
 }

2. NLog

NLog is a popular .NET Core logging framework that supports multiple output methods, including console, file, database, etc. NLog is characterized by simple configuration and easy use, and the log format and output method can be customized.

How to use

  1. Install NLog NuGet package:
Install-Package NLog
  1. Configure NLog at the program entry:
using NLog.Web;

 public static void Main(string[] args)
 {
     var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

     // ...
 }
  1. Log:
logger.Info("This is an information message.");
 logger.Warn("This is a warning message.");
 logger.Error("This is an error message.");

Sample code

using NLog.Web;

 public class Program
 {
     public static void Main(string[] args)
     {
         var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

         logger.Info("Hello, NLog!");

         LogManager.Shutdown();
     }
 }

3. Log4Net

Log4Net is a mature .NET Core logging framework that supports multiple output methods, including console, file, database, etc. Log4Net is characterized by high stability, flexible configuration, and the ability to customize log format and output methods.

How to use

  1. Install Log4Net NuGet package:
Install-Package log4net
  1. Configure Log4Net at the program entry:
using log4net;
 using log4net.Config;

 public static void Main(string[] args)
 {
     XmlConfigurator.Configure(new FileInfo("log4net.config"));

     var logger = LogManager.GetLogger(typeof(Program));

     // ...
 }
  1. Log:
logger.Info("This is an information message.");
 logger.Warn("This is a warning message.");
 logger.Error("This is an error message.");

Sample code

using log4net;using log4net.Config;

 public class Program
 {
     private static readonly ILog logger = LogManager.GetLogger(typeof(Program));

     public static void Main(string[] args)
     {
         XmlConfigurator.Configure(new FileInfo("log4net.config"));

         logger.Info("Hello, Log4Net!");
     }
 }

4. Microsoft.Extensions.Logging

Microsoft.Extensions.Logging is the logging framework that comes with .NET Core. It supports multiple output methods, including console, file, EventSource, etc. Microsoft.Extensions.Logging features easy integration and can be used with .NET Core’s dependency injection framework.

How to use

  1. Install the Microsoft.Extensions.Logging NuGet package:
Install-Package Microsoft.Extensions.Logging
  1. Configure Microsoft.Extensions.Logging at the program entry:
using Microsoft.Extensions.Logging;

 public static void Main(string[] args)
 {
     var loggerFactory = LoggerFactory.Create(builder =>
     {
         builder.AddConsole();
     });

     var logger = loggerFactory.CreateLogger();

     // ...
 }
  1. Log:
logger.LogInformation("This is an information message.");
 logger.LogWarning("This is a warning message.");
 logger.LogError("This is an error message.");

Sample code

using Microsoft.Extensions.Logging;

 public class Program
 {
     private static readonly ILogger logger;

     staticProgram()
     {
         var loggerFactory = LoggerFactory.Create(builder =>
         {
             builder.AddConsole();
         });

         logger = loggerFactory.CreateLogger();
     }

     public static void Main(string[] args)
     {
         logger.LogInformation("Hello, Microsoft.Extensions.Logging!");
     }
 }

5. Loupe

Loupe is a full-featured .NET Core logging framework that supports multiple output methods, including console, file, database, Web interface, etc. Loupe is characterized by its powerful functions and supports a variety of advanced functions such as analysis, monitoring, and alarming.

How to use

  1. Install the Loupe NuGet package:
Install-Package Gibraltar.Agent
  1. Configure Loupe at the program entry:
using Gibraltar.Agent;

 public static void Main(string[] args)
 {
     Log.StartSession();

     // ...
 }
  1. Log:
Log.Information("This is an information message.");
 Log.Warning("This is a warning message.");
 Log.Error("This is an error message.");

Sample code

using Gibraltar.Agent;

 public class Program
 {
     public static void Main(string[] args)
     {
         Log.StartSession();

         Log.Information("Hello, Loupe!");

         Log.EndSession();
     }
 }

The above is the introduction and usage of five excellent .NET Core logging frameworks. Each of these frameworks has its own characteristics, and you can choose the appropriate framework according to actual needs. No matter which framework you choose, remember to configure logs at the program entry and record logs in appropriate places to troubleshoot problems when they occur.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811213

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索