From 13836739c0a127b36b20aa280534f3e21876f84a Mon Sep 17 00:00:00 2001 From: Benjamin Ifland Date: Sun, 17 Mar 2019 21:52:44 +0100 Subject: [PATCH] add logging to SongController --- API/API/API.csproj | 1 + .../App_Start/DependencyInjectionConfig.cs | 4 ++-- API/API/Controllers/SongsController.cs | 15 ++++++++++++- API/API/Services/Logger.cs | 22 +++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 API/API/Services/Logger.cs diff --git a/API/API/API.csproj b/API/API/API.csproj index d2aff1a..692b34b 100644 --- a/API/API/API.csproj +++ b/API/API/API.csproj @@ -147,6 +147,7 @@ + diff --git a/API/API/App_Start/DependencyInjectionConfig.cs b/API/API/App_Start/DependencyInjectionConfig.cs index 0594403..58fbdfd 100644 --- a/API/API/App_Start/DependencyInjectionConfig.cs +++ b/API/API/App_Start/DependencyInjectionConfig.cs @@ -1,8 +1,8 @@ using API.Database; +using API.Services; using Autofac; using Autofac.Integration.WebApi; using ProductService.Controllers; -using System.Reflection; using System.Web.Http; namespace API.App_Start { @@ -28,7 +28,7 @@ namespace API.App_Start { } private static void RegisterServices(this ContainerBuilder builder) { - // ... builder.RegisterType(); + builder.RegisterType(); } private static void RegisterDataProvider(this ContainerBuilder builder) { diff --git a/API/API/Controllers/SongsController.cs b/API/API/Controllers/SongsController.cs index 9c410b0..1bcf76f 100644 --- a/API/API/Controllers/SongsController.cs +++ b/API/API/Controllers/SongsController.cs @@ -1,5 +1,6 @@ using API.Database; using API.Models; +using API.Services; using Microsoft.AspNet.OData; using System.Data.Entity; using System.Data.Entity.Infrastructure; @@ -10,28 +11,37 @@ using System.Web.Http; namespace ProductService.Controllers { public class SongsController : ODataController { - public SongsController(DataContext db) { + public SongsController( + DataContext db, + Logger logger + ) { this.db = db; + this.logger = logger; } private readonly DataContext db; + private readonly Logger logger; private bool ProductExists(int key) { + logger.Log($"{nameof(SongsController)}.{nameof(ProductExists)}({key})"); return db.Songs.Any(p => p.ID == key); } [EnableQuery] public IQueryable Get() { + logger.Log($"{nameof(SongsController)}.{nameof(Get)}()"); return db.Songs; } [EnableQuery] public SingleResult Get([FromODataUri] int key) { + logger.Log($"{nameof(SongsController)}.{nameof(Get)}({key})"); var result = db.Songs.Where(p => p.ID == key); return SingleResult.Create(result); } public async Task Post(Song song) { + logger.Log($"{nameof(SongsController)}.{nameof(Post)}()"); if (!ModelState.IsValid) return BadRequest(ModelState); db.Songs.Add(song); @@ -40,6 +50,7 @@ namespace ProductService.Controllers { } public async Task Patch([FromODataUri] int key, Delta product) { + logger.Log($"{nameof(SongsController)}.{nameof(Patch)}({key})"); if (!ModelState.IsValid) { return BadRequest(ModelState); } @@ -59,6 +70,7 @@ namespace ProductService.Controllers { } public async Task Put([FromODataUri] int key, Song update) { + logger.Log($"{nameof(SongsController)}.{nameof(Put)}({key})"); if (!ModelState.IsValid) return BadRequest(ModelState); if (key != update.ID) return BadRequest(); @@ -75,6 +87,7 @@ namespace ProductService.Controllers { } public async Task Delete([FromODataUri] int key) { + logger.Log($"{nameof(SongsController)}.{nameof(Delete)}({key})"); var product = await db.Songs.FindAsync(key); if (product == null) { return NotFound(); diff --git a/API/API/Services/Logger.cs b/API/API/Services/Logger.cs new file mode 100644 index 0000000..fb7fa4d --- /dev/null +++ b/API/API/Services/Logger.cs @@ -0,0 +1,22 @@ +using API.Database; +using API.Models; +using System; + +namespace API.Services { + public class Logger { + private readonly DataContext db; + + public Logger(DataContext db) { + this.db = db; + } + + public void Log(string method) { + var log = new Log { + Date = DateTime.Now, + Method = method + }; + db.Logs.Add(log); + db.SaveChanges(); + } + } +} \ No newline at end of file