- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain how logging works in ASP.NET Core
Logging is the process of recording events in software as they happen in real-time, along with other information such as the infrastructure details, time taken to execute, etc. Logging is an essential part of any software application. Having logs is crucial, especially when things go wrong. Logs help you understand the failures or performance bottlenecks and fix the problems.
Logs are typically written to a database, console, or file, depending on the severity of the application and convenience. Although it's possible to record any data in the logs, you write informational messages and error messages in general. The informational messages capture standard events as they happen, e.g. a method call, user authentication, product checkout, etc. The error messages capture the errors and provide all the data that might help you debug the program.
ASP.NET Core provides a generic logging interface to make it easy to log stuff from your application. The same interface is used throughout the framework and the third-party libraries, making it easier to search through the logs and diagnose the problem. The framework also allows you to configure the verbosity of your logs and send the logs to one or more destinations like file, console, or database.
In ASP.NET Core, logging providers store the logs. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).
You can override the default set of logging providers by calling ClearProviders and adding the specific logging providers that you need. The following code adds the console logging provider.
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(logging =>{ logging.ClearProviders(); logging.AddConsole(); }) .ConfigureWebHostDefaults(webBuilder =>{ webBuilder.UseStartup<Startup>(); });
You can create logs using the ILogger interface, which is injected into your code by the ASP.NET Core dependency injection container. The following example illustrates this.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; namespace Razor.Pages{ public class IndexModel : PageModel{ private readonly ILogger<IndexModel> _logger; public IndexModel(ILogger<IndexModel> logger){ _logger = logger; } public void OnGet(){ _logger.LogInformation("Index page visited."); } } }
Logging is essential for quickly diagnosing, troubleshooting, and fixing errors in a production application. It's a best practice to configure logging as soon as possible, ideally when you start developing your application.
- Related Articles
- Explain how reflection works in .NET framework
- Explain how error handling works in ASP.NET Core
- What is routing? Explain how it works in ASP.NET Core
- How to use ViewBag in ASP .Net MVC C#?
- What is ViewData in ASP .Net MVC C#?
- How to determine if C# .NET Core is installed?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- Explain how a rocket works.
- Explain how reset command works in Git
- Explain how Razor Pages work in ASP.NET Core
- Explain how the human ear works.
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- Explain how static files are served in ASP.NET Core
