From 4a385af3b5654a3524e0c84482e6d5df0613ee21 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sun, 5 May 2019 12:09:19 -0400 Subject: [PATCH] Implemented logging. To complete the logging implementation I have to log the flow to the log file #16 --- Controllers/SongController.cs | 23 ++++++++---- Icarus.csproj | 4 ++ Program.cs | 43 ++++++++++++++++----- Startup.cs | 70 ++++++++++++++++++----------------- appsettings.json | 5 ++- nlog.config | 33 +++++++++++++++++ 6 files changed, 126 insertions(+), 52 deletions(-) create mode 100644 nlog.config diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index f74578d..1c5bd09 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using Icarus.Controllers.Managers; using Icarus.Controllers.Utilities; @@ -20,6 +21,7 @@ namespace Icarus.Controllers public class SongController : ControllerBase { #region Fields + private readonly ILogger _logger; private IConfiguration _config; private MusicStoreContext _context; private SongManager _songMgr; @@ -31,10 +33,12 @@ namespace Icarus.Controllers #region Constructor - public SongController(IConfiguration config) + public SongController(IConfiguration config, ILogger logger) { _config = config; + _logger = logger; _songMgr = new SongManager(config); + _logger.LogInformation("Logging is working!"); } #endregion @@ -73,19 +77,24 @@ namespace Icarus.Controllers [HttpPut("{id}")] public IActionResult Put(int id, [FromBody] Song song) { - // TODO: Implement updating of song metadata MusicStoreContext context = HttpContext.RequestServices .GetService(typeof(MusicStoreContext)) as MusicStoreContext; + song.Id = id; Console.WriteLine("Retrieving filepath of song"); - var songPath = context.GetSong(id).SongPath; - song.SongPath = songPath; + var oldSongRecord = context.GetSong(id); + song.SongPath = oldSongRecord.SongPath; MetadataRetriever updateMetadata = new MetadataRetriever(); - updateMetadata.UpdateMetadata(song); + updateMetadata.UpdateMetadata(song, oldSongRecord); - context.UpdateSong(song); + var updatedSong = updateMetadata.UpdatedSongRecord; + context.UpdateSong(updatedSong); - SongResult songResult = new SongResult(); + SongResult songResult = new SongResult + { + Message = updateMetadata.Message, + SongTitle = updatedSong.Title + }; return Ok(songResult); } diff --git a/Icarus.csproj b/Icarus.csproj index f16d28e..4f3ca44 100644 --- a/Icarus.csproj +++ b/Icarus.csproj @@ -22,10 +22,14 @@ + + + + diff --git a/Program.cs b/Program.cs index 8035229..814a3f8 100644 --- a/Program.cs +++ b/Program.cs @@ -3,23 +3,46 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; + using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using NLog.Web; namespace Icarus { - public class Program - { - public static void Main(string[] args) - { - CreateWebHostBuilder(args).Build().Run(); - } + public class Program + { + public static void Main(string[] args) + { + var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup() - .UseUrls("http://localhost:5002"); + try + { + logger.Debug("init main"); + CreateWebHostBuilder(args).Build().Run(); + } + catch (Exception ex) + { + logger.Error(ex, "An error occurred"); + throw; + } + finally + { + NLog.LogManager.Shutdown(); + } + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup() + .UseUrls("http://localhost:5002") + .ConfigureLogging(logging => + { + logging.ClearProviders(); + logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); + }) + .UseNLog() ; } } diff --git a/Startup.cs b/Startup.cs index 75af88d..6bf9c36 100644 --- a/Startup.cs +++ b/Startup.cs @@ -18,6 +18,9 @@ using Microsoft.EntityFrameworkCore; using MySql.Data; using MySql.Data.EntityFrameworkCore.Extensions; using MySql.Data.MySqlClient; +using NLog; +using NLog.Web; +using NLog.Web.AspNetCore; using Icarus.Authorization; using Icarus.Authorization.Handlers; @@ -25,18 +28,18 @@ using Icarus.Models.Context; namespace Icarus { - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } - public IConfiguration Configuration { get; } + public IConfiguration Configuration { get; } - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSingleton(Configuration); @@ -80,34 +83,35 @@ namespace Icarus var connString = Configuration.GetConnectionString("DefaultConnection"); - services.Add(new ServiceDescriptor(typeof(MusicStoreContext), - new MusicStoreContext(Configuration - .GetConnectionString("DefaultConnection")))); - services.Add(new ServiceDescriptor(typeof(UserStoreContext), - new UserStoreContext(Configuration - .GetConnectionString("DefaultConnection")))); + services.Add(new ServiceDescriptor(typeof(MusicStoreContext), + new MusicStoreContext(Configuration + .GetConnectionString("DefaultConnection")))); + + services.Add(new ServiceDescriptor(typeof(UserStoreContext), + new UserStoreContext(Configuration + .GetConnectionString("DefaultConnection")))); services.AddDbContext(options => options.UseMySQL(connString)); services.AddDbContext(options => options.UseMySQL(connString)); - } + } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); - } + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } app.UseAuthentication(); - app.UseHttpsRedirection(); - app.UseMvc(); - } - } + app.UseHttpsRedirection(); + app.UseMvc(); + } + } } diff --git a/appsettings.json b/appsettings.json index b7772e6..9f29fea 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,12 +1,13 @@ { "Logging": { "LogLevel": { - "Default": "Warning" + "Default": "Trace", + "Microsoft": "Information" } }, "AllowedHosts": "*", "Auth0": { - "Domain": "[domain].auth0.com", + "Domain": "[domain].auth0.com", "ApiIdentifier": "https://[identifier]/api", "ClientId":"", "ClientSecret":"" diff --git a/nlog.config b/nlog.config new file mode 100644 index 0000000..34dc456 --- /dev/null +++ b/nlog.config @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +