using dotNET.Core;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text; namespace dotNET.EFCoreRepository
{
/// <summary>
/// ef 日志
/// </summary>
public class EFLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName) => new EFLogger(categoryName);
public void Dispose() { }
} /// <summary>
///
/// </summary>
public class EFLogger : ILogger
{
private readonly string categoryName; public EFLogger(string categoryName) => this.categoryName = categoryName; public bool IsEnabled(LogLevel logLevel) => true; public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
//ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
&& logLevel == LogLevel.Information)
{
var logContent = formatter(state, exception);
NLogger.Debug(logContent);
//TraceMessage("Something happened.");
// NLogger.Info(GetCodeLineAndFileName());
//TODO: 拿到日志内容想怎么玩就怎么玩吧
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(logContent);
Console.ResetColor();
}
} public IDisposable BeginScope<TState>(TState state) => null; public void TraceMessage(string message,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = )
{
NLogger.Debug("message: " + message);
NLogger.Debug("member name: " + memberName);
NLogger.Debug("source file path: " + sourceFilePath);
NLogger.Debug("source line number: " + sourceLineNumber);
} public string GetCodeLineAndFileName()
{
StackTrace insStackTrace = new StackTrace(true);
var insStackFrames = insStackTrace.GetFrames();
string str = "";
foreach(var insStackFrame in insStackFrames)
{
str += String.Format("\nFile: {0}, Line: {1}\n", insStackFrame.GetFileName(), insStackFrame.GetFileLineNumber());
}
return str;
} }
}

第一步: 添加日志类

第二步:OnConfiguring  方法添加日志调用

   protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(new EFLoggerProvider());
optionsBuilder.UseLoggerFactory(loggerFactory);
base.OnConfiguring(optionsBuilder);
}

请问在 .NET Core 中如何让 Entity Framework Core 在日志中记录由 LINQ 生成的SQL语句?的更多相关文章

  1. ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0

    ASP.NET 5.0 将改名为 ASP.NET Core 1.0 ASP.NET MVC 6  将改名为 ASP.NET MVC Core 1.0 Entity Framework 7.0    将 ...

  2. [转帖]2016年时的新闻:ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0

    ASP.NET Core 1.0.ASP.NET MVC Core 1.0和Entity Framework Core 1.0 http://www.cnblogs.com/webapi/p/5673 ...

  3. 在Apworks数据服务中使用基于Entity Framework Core的仓储(Repository)实现

    <在ASP.NET Core中使用Apworks快速开发数据服务>一文中,我介绍了如何使用Apworks框架的数据服务来快速构建用于查询和管理数据模型的RESTful API,通过该文的介 ...

  4. vs for Mac中的启用Entity Framework Core .NET命令行工具

    在vs for Mac的工具菜单中已没有了Package Manager Console. 我们可以通过以下方法使用Entity Framework Core .NET命令行工具: 1.添加Nuget ...

  5. ASP.NET Core 1.0: Using Entity Framework Core

    伴随着ASP.NET Core 1.0发布的还有Entity Framework Core 1.0; 官方文档链接:https://docs.efproject.net/en/latest/platf ...

  6. Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 38 Entity Framework ...

  7. [转]ASP.NET Core 1.0: Using Entity Framework Core 1.0 - Transaction

    本文转自:http://blog.csdn.net/alvachien/article/details/51576961 跟Entity Framework之前的版本不同,Class DbContex ...

  8. ASP.NET Core 1.0: Using Entity Framework Core 1.0 - Transaction

    跟Entity Framework之前的版本不同,Class DbContext不再有AcceptAllChanges()方法. 使用Transaction需要使用DbContext中的Databas ...

  9. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)

    在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...

随机推荐

  1. web模拟终端 --使用shellinabox

    关于shellinabox ShellInABox实现了一个Web服务器,可以将任意命令行工具导出到基于Web的终端仿真器.任何支持JavaScript和CSS的Web浏览器都可以访问此模拟器,并且不 ...

  2. centos 服务器 nginx 负载均衡服务安装

    yum -y install gcc gcc-c++ autoconf automake libtool make cmake yum -y install zlib zlib-devel opens ...

  3. Python 大文件处理

    非内存资源可以使用with 在python中逐行读取大文件 在我们日常工作中,难免会有处理日志文件的时候,当文件小的时候,基本不用当心什么,直接用file.read()或readlines()就可以了 ...

  4. flex布局--小实例

    圣杯布局(Holy Grail Layout)指的是一种最常见的网站布局.页面从上到下,分成三个部分:头部(header),躯干(body),尾部(footer).其中躯干又水平分成三栏,从左到右为: ...

  5. 项目Beta冲刺 随笔集合

    课程: 软件工程1916|W(福州大学) 作业要求: 项目Beta冲刺 团队名称: 火鸡堂 作业目标: 尽力交付 火鸡堂 队员学号 队员姓名 博客地址 备注 221600111 彼术向 http:// ...

  6. 【PHP】系统部署

    1.MySql数据库,单独创建用户和数据库 使用MySql-Front导入,避免使用Navicat导入 2.httpd-vhosts.conf配置 文件位于:D:\Xampp\apache\conf\ ...

  7. 将公式直接转化为Latex代码的神器-snip

    经常用latex写论文,免不了要敲各种公式,今天和大家分享一个神器-snip,它可以直接将公式转化为代码,不需要我们自己编写公式代码,方便快捷,准确率极高.该神器的下载地址为:https://math ...

  8. [React] Fix "React Error: Rendered fewer hooks than expected"

    In this lesson we'll see an interesting situation where we're actually calling a function component ...

  9. 在调试时,有什么更好的方法可以监视最后一个win32错误?

    我发现在代码中使用win32api时,需要多次监视最后一个win32错误!(在每次使用API后调用GetLastError()是不可行的解决方案!).. 在Visual Studio中,它们提供了一个 ...

  10. Linux后台运行和关闭程序、查看后台任务

    fg.bg.jobs.&.ctrl+z   1.&    (最经常被用到)     这个用在一个命令的最后,可以把这个命令放到后台执行   2.ctrl + z     可以将一个正在 ...