Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created May 12, 2021 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/97627fb043aeeadaf05a668ae5692a82 to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/97627fb043aeeadaf05a668ae5692a82 to your computer and use it in GitHub Desktop.
.NET Applications - Logging - Two ways to get the ILogger instances
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