EF Core使用Simple Logging输出日志

在使用EF Core的时候,很多时候需要知道EF Core实际执行的SQL语句是什么。

Simple Logging是EF Core提供的一项功能,可用于在开发和调试应用程序时轻松获取日志。这种形式的日志记录需要最少的配置,而不需要其他NuGet包。

功能一瞥

配置起来非常简单,只需在DbContext.OnConfiguring实现中调用LogTo方法即可:

public class DefaultDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        ...

        options.LogTo(Console.WriteLine);
    }
    ...
}

LogTo需要一个Action委托接受字符串,比如Console.WriteLine,你也可以编写自定义方法决定如何输出日志。

筛选

默认情况下,Simple Logging记录Debug或更高级别的每条日志。这样会导致输出的日志过多,对调试没有任何帮助,可以限制只记录Information或更高级别的日志:

options.LogTo(Console.WriteLine, 
    Microsoft.Extensions.Logging.LogLevel.Information);

查询标记

但是,这样还是会产生很多日志。这时我们可以结合查询标记,帮助我们快速定位到需要的日志:

 var users = context.User.TagWith("查询所有用户").ToList();

如果觉得这篇文章对你有所启发,请关注我的个人公众号”My IO“,记住我!

=> ERROR [master 4/8] RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir -r /app/ 2.9s ------ > [master 4/8] RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir -r /app/requirements.txt: 0.944 Looking in indexes: https://mirrors.aliyun.com/pypi/simple/ 1.175 Collecting scrapy_plus (from -r /app/requirements.txt (line 1)) 1.233 Downloading https://mirrors.aliyun.com/pypi/packages/ce/ab/8ff5db0cf85a46e581c3d5086ef0510225712e41e15cd52af1e0171e065e/scrapy_plus-1.0.5-py3-none-any.whl (29 kB) 1.323 Collecting pyhocon (from -r /app/requirements.txt (line 2)) 1.369 Downloading https://mirrors.aliyun.com/pypi/packages/39/2d/cd65dc4fa8c901e6d02b4074771ced04828d71af18b97da24ed1e55507d7/pyhocon-0.3.61-py3-none-any.whl (25 kB) 1.648 Collecting logging (from -r /app/requirements.txt (line 3)) 1.958 Downloading https://mirrors.aliyun.com/pypi/packages/93/4b/979db9e44be09f71e85c9c8cfc42f258adfb7d93ce01deed2788b2948919/logging-0.4.9.6.tar.gz (96 kB) 2.030 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.0/96.0 kB 1.3 MB/s eta 0:00:00 2.062 Preparing metadata (setup.py): started 2.451 Preparing metadata (setup.py): finished with status 'error' 2.462 error: subprocess-exited-with-error 2.462 2.462 × python setup.py egg_info did not run successfully. 2.462 │ exit code: 1 2.462 ╰─> [21 lines of output] 2.462 Traceback (most recent call last): 2.462 File "<string>", line 2, in <module> 2.462 File "<pip-setuptools-caller>", line 14, in <module> 2.462 File "/usr/local/lib/python3.10/dist-packages/setuptools/__init__.py", line 8, in <module> 2.462 import _distutils_hack.override # noqa: F401 2.462 File "/usr/local/lib/python3.10/dist-packages/_distutils_hack/override.py", line 1, in <module> 2.462 __import__('_distutils_hack').do_override() 2.462 File "/usr/local/lib/python3.10/dist-packages/_distutils_hack/__init__.py", line 70, in do_override 2.462 ensure_local_distutils() 2.462 File "/usr/local/lib/python3.10/dist-packages/_distutils_hack/__init__.py", line 56, in ensure_local_distutils 2.462 core = importlib.import_module('distutils.core') 2.462 File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module 2.462 return _bootstrap._gcd_import(name[level:], package, level) 2.462 File "/usr/local/lib/python3.10/dist-packages/setuptools/_distutils/core.py", line 13, in <module> 2.462 from .cmd import Command 2.462 File "/usr/local/lib/python3.10/dist-packages/setuptools/_distutils/cmd.py", line 7, in <module> 2.462 import logging 2.462 File "/tmp/pip-install-h6uopuco/logging_03f85449da774c7fa6e3ec74f615ec0a/logging/__init__.py", line 618 2.462 raise NotImplementedError, 'emit must be implemented '\ 2.462 ^ 2.462 SyntaxError: invalid syntax 2.462 [end of output] 2.462 2.462 note: This error originates from a subprocess, and is likely not a problem with pip. 2.466 error: metadata-generation-failed 2.466 2.466 × Encountered error while generating package metadata. 2.466 ╰─> See above for output. 2.466 2.466 note: This is an issue with the package mentioned above, not pip. 2.466 hint: See above for details. 2.778 2.778 [notice] A new release of pip is available: 24.0 -> 25.0.1 2.778 [notice] To update, run: python3.10 -m pip install --upgrade pip ------ failed to solve: process "/bin/sh -c pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir -r /app/requirements.txt" did not complete successfully: exit code: 1
最新发布
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值