Here are the steps to use log4Net:
1. Right click on your NorthwindCafe.Web project and select "Manage NuGet Packages"
2. Search for Microsoft.Extensions.Logging.Log4Net.AspNetCore
3. Click "Install", click "OK" on the preview changes pop-up, and click on "Accept" on the license screen
4. Create a file call log4net.config in the root folder of the project with the following markup
<?xml version="1.0" encoding="utf-8" ?><log4net><appender name="DebugAppender" type="log4net.Appender.DebugAppender" ><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /></layout></appender><appender name="RollingFile" type="log4net.Appender.RollingFileAppender"><file value="NorthwindCafe.log" /><appendToFile value="true" /><maximumFileSize value="100KB" /><maxSizeRollBackups value="2" /><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" /></layout></appender><root><level value="ALL"/><appender-ref ref="DebugAppender" /><appender-ref ref="RollingFile" /></root></log4net>
5. Open the Startup.cs file and change the Configure method to add log4net support with this line loggerFactory.AddLog4Net
First you have to import the package using Microsoft.Extensions.Logging; using the using statement
Here is the entire method, you have to prefix the ILoggerFactory interface with the namespace
public void Configure(IApplicationBuilder app, IHostingEnvironment env, NorthwindContext context, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory)
{
loggerFactory.AddLog4Net();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvc(config => {
config.MapRoute(
name: "Default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" }
);
});
DbInitializer.Initialize(context);
}
6. Now go to the HomeController and add log4net to it by injecting it in the constructor
private ILogger<HomeController> _logger;
public HomeController(IProductRepository repository, ILogger<HomeController> logger)
{
_repository = repository;
_logger = logger;
}
The entire HomeController code should look like this up to this point.
using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Logging;using NorthwindCafe.Web.Data;namespace NorthwindCafe.Web.Controllers{public class HomeController : Controller{private readonly IProductRepository _repository;private readonly ILogger<HomeController> _logger;public HomeController(IProductRepository repository, ILogger<HomeController> logger){_repository = repository;_logger = logger;}public IActionResult Index(){var products = _repository.GetProducts();return View(products);}public IActionResult About(){return View();}public IActionResult Contact(){return View();}}}
7. Now if you run the application you will see the NorthwindCafe.log file in the application. You can change the log level in the log4net.config file.
github: https://github.com/techjunkiejh/NorthwindCafe/tree/Hou17
Previous: Hour 16: ASP.NET Core Display Products
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeleteVé máy bay đi Mỹ
từ mỹ về việt nam được chưa
giá vé máy bay từ nhật về việt nam
vé máy bay từ frankfurt đi hà nội
thông tin chuyến bay từ canada về việt nam
gia ve may bay tu han quoc ve viet nam
vé máy bay cho chuyên gia nước ngoài
Great post! tableau course
ReplyDelete