Wednesday, August 28, 2019
A lot of examples on logging in ASP.NET Core shows you how to add console logging to your ASP.NET Core application. But it's pretty useless in a real world application. What you really want is to write to a file or a database. There's a package called Microsoft.Extensions.Logging.Log4Net.AspNetCore that will make your application use log4Net.
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
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
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)
The entire HomeController code should look like this up to this point.
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
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
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
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