Logging
Using NLog Logger
NLog.Extensions.Logging is the official NLog provider for Microsoft’s in .NET Core and ASP.NET Core. Here and here are instruction and example respectively.
Add Logger to Controller
Instead of requesting an ILoggerFactory and creating an instance of ILogger explicitly, you can request an ILogger
public class TodoController : Controller
{
private readonly ILogger _logger;
public TodoController(ILogger<TodoController> logger)
{
_logger = logger;
}
}
Using Serilog in ASP.NET core 1.0 application
1)In project.json, add below dependencies-
"Serilog": "2.2.0",
"Serilog.Extensions.Logging": "1.2.0",
"Serilog.Sinks.RollingFile": "2.0.0",
"Serilog.Sinks.File": "3.0.0"
2)In Startup.cs, add below lines in constructor-
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.RollingFile(Path.Combine(env.ContentRootPath, "Serilog-{Date}.txt"))
.CreateLogger();
3)In Configure method of Startup class-
loggerFactory.AddSerilog();
4)In Controller, create instance of ILogger like this-
public class HomeController : Controller
{
ILogger<HomeController> _logger = null;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
5)Sample logging below-
try
{
throw new Exception("Serilog Testing");
}
catch (System.Exception ex)
{
this._logger.LogError(ex.Message);
}