Skip to content

Instantly share code, notes, and snippets.

@jellever jellever/EFExtensions.cs
Last active Sep 9, 2015

Embed
What would you like to do?
Entity Framework 7, update 6 query logging snippet
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
namespace MyNameSpace
{
public static class EFExtensions
{
public static void EnableDebugLog(this DbContext context)
{
IServiceProvider service = ((IAccessor<IServiceProvider>)context).Service;
ILoggerFactory loggerFactory = service.GetRequiredService<ILoggerFactory>();
loggerFactory.AddProvider(new LoggerProvider());
}
}
class LoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string name)
{
return new DebugLogger(name);
}
}
//https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Framework.Logging.Debug/DebugLogger.cs
/// <summary>
/// A logger that writes messages in the debug output window only when a debugger is attached.
/// </summary>
public partial class DebugLogger : ILogger
{
private readonly Func<string, LogLevel, bool> _filter;
private readonly string _name;
/// <summary>
/// Initializes a new instance of the <see cref="DebugLogger"/> class.
/// </summary>
/// <param name="name">The name of the logger.</param>
public DebugLogger(string name)
: this(name, filter: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DebugLogger"/> class.
/// </summary>
/// <param name="name">The name of the logger.</param>
/// <param name="filter">The function used to filter events based on the log level.</param>
public DebugLogger(string name, Func<string, LogLevel, bool> filter)
{
_name = string.IsNullOrEmpty(name) ? nameof(DebugLogger) : name;
_filter = filter;
}
/// <inheritdoc />
public IDisposable BeginScopeImpl(object state)
{
return new NoopDisposable();
}
/// <inheritdoc />
public bool IsEnabled(LogLevel logLevel)
{
// If the filter is null, everything is enabled
// unless the debugger is not attached
return Debugger.IsAttached &&
(_filter == null || _filter(_name, logLevel));
}
/// <inheritdoc />
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
string message;
var values = state as ILogValues;
if (formatter != null)
{
message = formatter(state, exception);
}
else if (values != null)
{
message = LogFormatter.FormatLogValues(values);
if (exception != null)
{
message += Environment.NewLine + exception;
}
}
else
{
message = LogFormatter.Formatter(state, exception);
}
if (string.IsNullOrEmpty(message))
{
return;
}
message = $"{ logLevel }: {message}";
Debug.WriteLine(message, _name);
}
private class NoopDisposable : IDisposable
{
public void Dispose()
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.