Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
SeriLog
Browse files Browse the repository at this point in the history
  • Loading branch information
ActiveVibe committed Dec 6, 2017
1 parent a86b65b commit 3bb2d13
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Bunt.Core/Bunt.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<PackageReference Include="MediatR" Version="4.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.0.1" />
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.0" />
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.1.0-dev-00174" />
</ItemGroup>

</Project>
7 changes: 6 additions & 1 deletion Bunt.Core/Domain/Commands/SparaBuntladeStalle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Bunt.Core.Domain.Model;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Serilog;

namespace Bunt.Core.Domain.Commands
{
Expand All @@ -19,10 +20,12 @@ public class Command : IRequest
public class Handler : IRequestHandler<Command>
{
private readonly BuntDbContext _db;
private readonly ILogger _logger;

public Handler(BuntDbContext db)
public Handler(BuntDbContext db, ILogger logger)
{
_db = db;
_logger = logger;
}

public async Task Handle(Command command, CancellationToken cancellationToken)
Expand All @@ -33,11 +36,13 @@ public async Task Handle(Command command, CancellationToken cancellationToken)
{
var lastIndex = await _db.BuntladeStallen.MaxAsync(b => b.Index, cancellationToken);
buntladeStalle = new BuntladeStalle(command.Id, command.Adress, command.Typ, lastIndex + 1);
_logger.Information("Nytt buntlådeställe skapat med id: {Id}", buntladeStalle.Id);
_db.Add(buntladeStalle);
}
else
{
buntladeStalle.Redigera(command.Adress, command.Typ);
_logger.Information("Uppdaterade befintligt buntådeställe med id: {Id}", buntladeStalle.Id);
}

await _db.SaveChangesAsync(cancellationToken);
Expand Down
36 changes: 36 additions & 0 deletions Bunt.Core/Infrastructure/LoggingBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Serilog;
using Serilog.Context;

namespace Bunt.Core.Infrastructure
{
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly ILogger _logger;

public LoggingBehavior(ILogger logger)
{
_logger = logger;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
using (LogContext.PushProperty("CorrelationId", Guid.NewGuid()))
using (LogContext.PushProperty("RequestName", typeof(TRequest).FullName))
{
try
{
_logger.Information("Executing {@RequestName} with payload {@RequestPayload}", typeof(TRequest).FullName, request);
return await next();
}
catch (Exception e)
{
_logger.Error(e, e.Message);
throw;
}
}
}
}
}
29 changes: 29 additions & 0 deletions Bunt.Migrations/201712061300_AddLogTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FluentMigrator;

namespace Bunt.Migrations
{
[Migration(201712061300)]
public class AddRequestLogTable : Migration
{
public override void Up()
{
Execute.Sql(@"
CREATE TABLE [Log] (
[Id] int IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Message] nvarchar(max) NULL,
[MessageTemplate] nvarchar(max) NULL,
[Level] nvarchar(128) NULL,
[TimeStamp] datetimeoffset(7) NOT NULL, -- use datetime for SQL Server pre-2008
[Exception] nvarchar(max) NULL,
[Properties] xml NULL,
[LogEvent] nvarchar(max) NULL
)
");
}

public override void Down()
{
Delete.Table("Log");
}
}
}
8 changes: 8 additions & 0 deletions Bunt.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;

namespace Bunt.Web
{
Expand All @@ -27,6 +28,13 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var logger = new LoggerConfiguration()
.WriteTo.MSSqlServer(Configuration.GetConnectionString("BuntDb"), "Log")
.Enrich.FromLogContext()
.CreateLogger();

services.AddSingleton<ILogger>(logger);
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
services.AddTransient<IConnectionFactory>(provider => new SqlConnectionFactory(Configuration.GetConnectionString("BuntDb")));
services.AddDbContext<BuntDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BuntDb")));
services.AddMediatR(typeof(ListaBuntladeStallen.Handler));
Expand Down

0 comments on commit 3bb2d13

Please sign in to comment.