Created
May 12, 2021 18:47
-
-
Save manoj-choudhari-git/97627fb043aeeadaf05a668ae5692a82 to your computer and use it in GitHub Desktop.
.NET Applications - Logging - Two ways to get the ILogger instances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
private readonly ILogger<HomeController> _logger; | |
private readonly ILogger _loggerWithSpecifiedCategory; | |
public HomeController(ILogger<HomeController> logger, ILoggerFactory loggerFactory) | |
{ | |
// Log category is Full Qualified Name of HomeController class | |
_logger = logger; | |
// Log category is "SomeCategory" | |
_loggerWithSpecifiedCategory = loggerFactory.CreateLogger("SomeCategory"); | |
} | |
public IActionResult Index() | |
{ | |
_logger.LogInformation("Info Log"); | |
_logger.LogWarning("Warning Log"); | |
_loggerWithSpecifiedCategory.LogError("Error Log"); | |
_loggerWithSpecifiedCategory.LogCritical("Critical Log"); | |
return View(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment