Background: ASP.NET 5 (ASP.NET Core 1.0) MVC 6 application using Dapper and the Repository Pattern
Obviously, like with every other website/app, I\'m trying to elimi
The trick to doing this is not in the attribute - it's by adding a middleware provider. Once your middleware is in the pipeline, you'll be able to catch exceptions thrown at any point (so your attribute will no longer be needed).
This is the thing that's actually going to log errors. I've copied what I've seen from your IErrorRepo
interface, but of course you could modify it to include any of the additional information passed into the Log
method below.
public class UnhandledExceptionLogger : ILogger
{
private readonly IErrorRepo _repo;
public UnhandledExceptionLogger(IErrorRepo repo)
{
_repo = repo;
}
public IDisposable BeginScopeImpl(object state) =>
new NoOpDisposable();
public bool IsEnabled(LogLevel logLevel) =>
logLevel == LogLevel.Critical || logLevel == LogLevel.Error;
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func
This is the factory that will create the logger. It's only going to be instantiated once, and will create all the loggers when it goes through the pipeline.
public class UnhandledExceptionLoggerProvider : ILoggerProvider
{
private readonly IErrorRepo _repo;
public UnhandledExceptionLoggerProvider(IErrorRepo repo)
{
_repo = repo;
}
public ILogger CreateLogger(string categoryName) =>
new UnhandledExceptionLogger(_repo);
public void Dispose()
{
}
}
Once added to the ILoggerFactory
, it will be invoked on each request in the pipeline. Often this is done through a custom extension method, but we've already got a lot of new code so I'll omit that part.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddProvider(new UnhandledExceptionLoggerProvider(new ErrorRepo()));
// the rest