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
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