Tech Junkie Blog: ASP.NET Core: Add Logging To The NorthwindCafe Application

Wednesday, March 29, 2017

ASP.NET Core: Add Logging To The NorthwindCafe Application

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


        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:
  1. How To Create An ASP.NET Core Application From Scratch
  2. ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
  3. Enable ASP.NET Core to Serve Static Files
  4. Enable MVC On ASP.NET Core Application
  5. ASP.NET Core : Create Our First Controller and View  
  6. ASP.NET Core : Adding The Default View With _ViewStart.cshtml
  7. ASP.NET Core : Create A Responsive Layout With Bootstrap
  8. ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
  9. ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
  10. ASP.NET Core : Entity Framework Core Models For Northwind Cafe
  11. ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
  12. ASP.NET Core : Configure project.json File To Support Entity Framework Core
  13. ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
  14. ASP.NET Core: Seeding The NorwindCafe Database
  15. ASP.NET Core: Add Logging To The NorthwindCafe Application

No comments:

Post a Comment