Skip to content

Latest commit

 

History

History
157 lines (121 loc) · 5.67 KB

Logging.md

File metadata and controls

157 lines (121 loc) · 5.67 KB

Logging

Logging is an essential part of any application, as it helps developers track down issues, gather information about the system, and monitor application performance. ASP.NET Core has a built-in logging framework that is easy to set up and use. The framework is extensible, allowing you to plug in third-party logging providers if needed.

1. Configure Logging:

ASP.NET Core uses the ILoggerFactory to create ILogger instances. You can configure logging in the Program.cs file, using the ConfigureLogging method.

Here's an example of configuring logging in an ASP.NET Core application:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddConsole();
                logging.AddDebug();
                logging.AddEventSourceLogger();
                logging.SetMinimumLevel(LogLevel.Information);
            });
}

In this example, we clear all default providers and add the Console, Debug, and EventSource logging providers. We also set the minimum log level to Information.

2. Use ILogger in your code:

To use logging in your code, inject an ILogger<T> instance into your classes, where T is the type of the class you are injecting the logger into. For example, to use logging in a controller, do the following:

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation("Home/Index called");
        return View();
    }

    // Other action methods
}

In this example, we inject an ILogger<HomeController> instance and log an informational message when the Index action is called.

3. Log Levels:

ASP.NET Core supports six log levels:

  • Trace: Most detailed log messages, used for debugging and diagnostics.
  • Debug: Detailed log messages, typically used for development purposes.
  • Information: General informational messages about the application's normal operation.
  • Warning: Indicates a potential issue, but the application can continue to function.
  • Error: Indicates a serious issue that requires immediate attention.
  • Critical: Indicates a critical failure that has caused the application to terminate.

You can use different log levels to filter log messages depending on your needs. For example, you might want to log only Warning and higher messages in production, while logging Debug and higher messages during development.

4. Structured Logging:

ASP.NET Core supports structured logging, which allows you to include additional data with your log messages. This can be useful for filtering and querying log data. To use structured logging, include placeholders in your log message and pass the corresponding values as arguments:

_logger.LogInformation("User {UserId} logged in at {LoginTime}", userId, DateTime.UtcNow);

In this example, the UserId and LoginTime are included as structured data in the log message.

5. Third-party Logging Providers:

ASP.NET Core supports a wide range of third-party logging providers, such as Serilog, NLog, and log4net. You can easily integrate these providers into your application by installing the corresponding NuGet packages and configuring them in the Program.cs file.

For example, to use Serilog, install the Serilog.AspNetCore NuGet package, and then update your Program.cs file as follows:

using Serilog;
using Serilog.Events;

public class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .CreateLogger();

        try
        {
            Log.Information("Starting web host");
            CreateHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Host terminated unexpectedly");
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog() // Use Serilog as the logging provider
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

In this example, we configure Serilog to log messages to the console and set the minimum log level to Debug for our application and Information for Microsoft's log messages. We also enrich the log entries with additional context data.

With Serilog configured, you can use it in the same way you would use the built-in ILogger<T> instances:

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation("Home/Index called");
        return View();
    }

    // Other action methods
}

ASP.NET Core's built-in logging framework is flexible and powerful, making it easy to integrate with various logging providers and to configure logging to meet your application's needs.