Skip to content

Commit

Permalink
Wrapped server / request logging in their own transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcaisse committed Apr 12, 2018
1 parent edf4b61 commit 39ff328
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 29 deletions.
23 changes: 23 additions & 0 deletions CarTracker/CarTracker.Data/CarTrackerDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace CarTracker.Data
{
public class CarTrackerDbContextFactory
{
private readonly DbContextOptions<CarTrackerDbContext> _options;

public CarTrackerDbContextFactory(DbContextOptions<CarTrackerDbContext> options)
{
_options = options;
}

public CarTrackerDbContext CreateContext()
{
return new CarTrackerDbContext(_options);
}

}
}
62 changes: 35 additions & 27 deletions CarTracker/CarTracker.Logic/Services/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace CarTracker.Logic.Services
public class Logger : IRequestLogger, IServerLogger
{

private readonly CarTrackerDbContext _db;
private readonly CarTrackerDbContextFactory _dbFactory;
private readonly IRequestInformation _requestInformation;
private readonly Guid _eventGuid;

public Logger(CarTrackerDbContext db, IRequestInformation requestInformation)
public Logger(CarTrackerDbContextFactory dbFactory, IRequestInformation requestInformation)
{
_db = db;
_dbFactory = dbFactory;
_requestInformation = requestInformation;
_eventGuid = Guid.NewGuid();
}
Expand All @@ -29,22 +29,26 @@ public Logger(CarTrackerDbContext db, IRequestInformation requestInformation)
IEnumerable requestHeaders, string requestBody, string responseStatus,
IEnumerable responseHeaders, string responseBody)
{
var requestLog = new RequestLog()
using (var db = _dbFactory.CreateContext())
{
ClientAddress = clientAddress,
RequestUrl = requestUrl,
RequestMethod = requestMethod,
RequestHeaders = JsonConvert.SerializeObject(requestHeaders),
RequestBody = requestBody,
ResponseStatus = responseStatus,
ResponseHeaders = JsonConvert.SerializeObject(responseHeaders),
ResponseBody = responseBody,
Type = LogType.Debug,
RequestUuid = _eventGuid,
UserId = _requestInformation.UserId
};
_db.RequestLogs.Add(requestLog);
_db.SaveChanges();
var requestLog = new RequestLog()
{
ClientAddress = clientAddress,
RequestUrl = requestUrl,
RequestMethod = requestMethod,
RequestHeaders = JsonConvert.SerializeObject(requestHeaders),
RequestBody = requestBody,
ResponseStatus = responseStatus,
ResponseHeaders = JsonConvert.SerializeObject(responseHeaders),
ResponseBody = responseBody,
Type = LogType.Debug,
RequestUuid = _eventGuid,
UserId = _requestInformation.UserId
};
db.RequestLogs.Add(requestLog);
db.SaveChanges();
}

}

public void Debug(string message)
Expand Down Expand Up @@ -89,17 +93,21 @@ public void Error(Exception e)

protected void CreateServerLog(LogType type, string message, Exception e = null)
{
var serverLog = new ServerLog()
using (var db = _dbFactory.CreateContext())
{
RequestUuid = _eventGuid,
Message = message,
ExceptionMessage = e?.Message,
Type = type,
StackTrace = e?.StackTrace
};
var serverLog = new ServerLog()
{
RequestUuid = _eventGuid,
Message = message,
ExceptionMessage = e?.Message,
Type = type,
StackTrace = e?.StackTrace
};

_db.ServerLogs.Add(serverLog);
_db.SaveChanges();
db.ServerLogs.Add(serverLog);
db.SaveChanges();
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CarTracker.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace CarTracker.Web.Extensions
{
public static class CarTrackerDbContextFactoryExtension
{

public static IServiceCollection AddCarTrackerDbContextFactory(this IServiceCollection serviceCollection,
Action<DbContextOptionsBuilder> optionsAction)
{
var builder = new DbContextOptionsBuilder<CarTrackerDbContext>();
optionsAction(builder);

serviceCollection.AddSingleton(new CarTrackerDbContextFactory(builder.Options));
return serviceCollection;
}

}
}
9 changes: 7 additions & 2 deletions CarTracker/CarTracker.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using CarTracker.Scheduler.Jobs;
using CarTracker.Web.Auth;
using CarTracker.Web.Configuration;
using CarTracker.Web.Extensions;
using CarTracker.Web.Middleware;
using CarTracker.Web.Model;
using CarTracker.Web.Util;
Expand Down Expand Up @@ -57,8 +58,12 @@ public Startup(IHostingEnvironment env)
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddDbContext<CarTrackerDbContext>(
options => options.UseMySql(Configuration.GetSection("connectionString").Value));

Action<DbContextOptionsBuilder> dbOptions =
options => options.UseMySql(Configuration.GetSection("connectionString").Value);

services.AddCarTrackerDbContextFactory(dbOptions);
services.AddDbContext<CarTrackerDbContext>(dbOptions);

//Add the build information
var buildInformation = Configuration.GetSection("buildInformation").Get<BuildInformation>();
Expand Down

0 comments on commit 39ff328

Please sign in to comment.