我想看看EF正在执行什么……它在新项目中运行良好,但我似乎无法让serilog打印它? 我这样设置覆盖;

var host = Host.CreateDefaultBuilder()
    .ConfigureServices((context, collection) => ServerServiceCollection.AddServices(collection, context.Configuration))
    .UseSerilog((hostContext, _, logger) => 
        logger.ReadFrom.Configuration(hostContext.Configuration)
        .MinimumLevel.Override("Microsoft", LogEventLevel.Verbose))
    .Build();

推荐答案

在您的MyCustomDB上下文中.

通过构造函数输入以下内容:

Microsoft.Extensions.Logging.ILoggerFactory

(这是为了向接口编码,而不是具体的,Serilog是具体的)

然后使用您注入的ILoggerFactory.将"作为生成的SQL陈述的目标".如下所示:

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.EntityFrameworkCore;


public class MyCustomDbContext : DbContext
{
    public const string ErrorMessageILoggerFactoryWrapperIsNull = "ILoggerFactory is null";

    private readonly Microsoft.Extensions.Logging.ILoggerFactory loggerFactory;

    public MyCustomDbContext(
        ILoggerFactoryWrapper loggerFactory,
        DbContextOptions<MyCustomDbContext> options)
        : base(options)
    {
        this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(
            ErrorMessageILoggerFactoryWrapperIsNull,
            (Exception)null);

        /* note, the base-class checks for null for the DbContextOptions-"options" */

    }


        // your "DbSet's" HERE, now shown.



    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);

        // null is possible if you are using an IDesignTimeDbContextFactory
        // below, you should also consider the use of IHostEnvironment values .. to avoid putting sql-statements in production.  see : https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.hostenvironmentenvextensions.isdevelopment?view=dotnet-plat-ext-8.0



        if (null != this.loggerFactory)
        {
            //// NOT SHOWN :::  Probably should not log sql statements in production, so use hostEnvironment "flags" to only do it when a developer is involved.
            ///but below..is the primary "magic" to get Sql statements to show up.
            optionsBuilder.UseLoggerFactory(this.loggerFactory);
            
            // other non-related options shown above
            //optionsBuilder.EnableSensitiveDataLogging();
            //optionsBuilder.EnableDetailedErrors();
        }
    }

当然..在你知道后(通过这个回答)..神奇的方法名称..很容易在互联网上搜索并找到这样的文章:

https://www.infoworld.com/article/3679528/how-to-work-with-logging-in-entity-framework-core-7.html

而且..在微软.

https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/extensions-logging?tabs=v3#other-application-types

恕我直言,不要落入"简单"的诱惑.(在微软文章中). 花几分钟时间连接您的"真正"ILoggerFactory / ILogger.

写入"控制台".是短视的.

Csharp相关问答推荐

C#中的两个线程之间读写浮点类型

将C#字符串转换为其UTF8编码字符的十六进制表示

当MD5被废弃时,如何在Blazor WASM中使用它?

等待限制选项似乎不适用于频道

AutoMapper -如何为两个不同的用例设置单个映射?

Blazor:类型或命名空间名称Components在命名空间中不存在''

找不到网址:https://localhost:7002/Category/Add?区域= Admin.为什么我的URL是这样生成的?area = Admin而不是/Admin/

TDLib与机器人共享电话号码

Unity 2D自顶向下弓旋转

在LINQ Where子句中使用新的DateTime

EFR32BG22 BLE在SPP模式下与PC(Windows 10)不连接

如何在我的C#应用程序中设置带有reactjs前端的SignalR服务器?

等待一个等待函数

在使用StringBuilder时,如何根据 colored颜色 设置为richTextBox中的特定行着色?

为什么Azure函数(独立工作进程)索引失败?使用Azure App配置的CosmosDbTrigger绑定失败,未解析为值

如何从Entity Framework Core中填充ListIInterface

将C#类导入到PowerShell

ASP.NET Core 8 Web API:如何添加版本控制?

如何根据分割文本的块数来计算文本的大小?

从具有泛型类型约束的类继承-Blazor