Wednesday, March 29, 2017
Logging is a good service to add as the application gets more complicated. It will allow us to see what the error is when things goes wrong.
To add logging to our ASP.NET Core application follow the steps below:
1. Open the Startup.cs file and add the logging service to the ConfigureServices method with the line
services.AddLogging();
So your ConfigureServices method should look like the following
2. In the Configure method type in the following line
if(env.IsDevelopment()) { app.UseDeveloperExceptionPage(); loggerFactory.AddConsole(LogLevel.Information); } else { loggerFactory.AddConsole(LogLevel.Error); }
The code above tells the application to use the developer's exception page if the environment is development and log level is set at information, else just log the error. Your Configure method should look like the following
To use the logger in the controller just add it to the constructor, example if we want to use logging in our HomeController we can just pass it into the constructor like so
To add logging to our ASP.NET Core application follow the steps below:
1. Open the Startup.cs file and add the logging service to the ConfigureServices method with the line
services.AddLogging();
So your ConfigureServices method should look like the following
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); var connectionString = Startup.Configuration["Data:NorthwindContextConnection"]; services.AddDbContext(options => options.UseSqlServer(connectionString)); services.AddLogging(); }
2. In the Configure method type in the following line
if(env.IsDevelopment()) { app.UseDeveloperExceptionPage(); loggerFactory.AddConsole(LogLevel.Information); } else { loggerFactory.AddConsole(LogLevel.Error); }
The code above tells the application to use the developer's exception page if the environment is development and log level is set at information, else just log the error. Your Configure method should look like the following
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, NorthwindContext context) { app.UseStaticFiles(); app.UseMvc(config => { config.MapRoute( name: "Default", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" } ); }); if(env.IsDevelopment()) { app.UseDeveloperExceptionPage(); loggerFactory.AddConsole(LogLevel.Information); } else { loggerFactory.AddConsole(LogLevel.Error); } DbInitializer.Initialize(context); }
To use the logger in the controller just add it to the constructor, example if we want to use logging in our HomeController we can just pass it into the constructor like so
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 namespace NorthwindCafe.Web.Controllers { public class HomeController : Controller { private ILogger_logger; public HomeController(ILogger logger) { _logger = logger; } // GET: / / public IActionResult Index() { return View(); } } }
ASP.NET Core Posts:
- How To Create An ASP.NET Core Application From Scratch
- ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
- Enable MVC On ASP.NET Core Application
- ASP.NET Core : Create Our First Controller and View
- ASP.NET Core : Adding The Default View With _ViewStart.cshtml
- ASP.NET Core : Create A Responsive Layout With Bootstrap
- ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
- ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
- ASP.NET Core : Entity Framework Core Models For Northwind Cafe
- ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
- ASP.NET Core : Configure project.json File To Support Entity Framework Core
- ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
- ASP.NET Core: Seeding The NorwindCafe Database
- ASP.NET Core: Add Logging To The NorthwindCafe Application
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
ReplyDeletegiá vé máy bay đi Mỹ khứ hồi
có thể bay từ mỹ về việt nam không
khi nào có chuyến bay từ nhật về việt nam
khi nào có chuyến bay từ đức về việt nam
ve may bay tu canada ve viet nam
vé máy bay từ hàn quốc sang việt nam
chuyến bay dành cho chuyên gia
Thanks for sharing this.,
ReplyDeletecertified scrum master certification
agile scrum master certification