Microsoft.Extensions.Logging 是一種可擴展的記錄機制,具有許多常見記錄系統的提供者插件。 Microsoft 提供的外掛程式 (例如 Microsoft.Extensions.Logging.Console) 和協力廠商外掛程式 (例如 Serilog.Extensions.Logging) 都可作為 NuGet 套件使用。
Entity Framework Core (EF Core) 完全整合與 Microsoft.Extensions.Logging。 不過,請考慮使用 簡單記錄 ,以取得更簡單的記錄方式,特別是對於不使用相依性插入的應用程式。
ASP.NET Core 應用程式
Microsoft.Extensions.Logging 預設 會在 ASP.NET 核心應用程式中使用。 呼叫 AddDbContext 或 AddDbContextPool 讓 EF Core 自動使用透過一般 ASP.NET 機制設定的記錄設定。
其他應用程式類型
其他應用程式類型可以使用 GenericHost 來取得與 ASP.NET Core 中使用的相同相依性插入模式。 AddDbContext 或者 AddDbContextPool 可以像在 ASP.NET Core 應用程序中一樣使用。
Microsoft.Extensions.Logging 也可用於不使用相依性注入的應用程式,不過 簡單的記錄 可能更容易設定。
Microsoft.Extensions.Logging 需要建立 LoggerFactory. 此工廠應該被儲存為某個靜態/全域實例,並在每次建立 DbContext 時使用。 例如,通常會將記錄器工廠作為靜態屬性儲存在 DbContext 上。
public static readonly ILoggerFactory MyLoggerFactory
= LoggerFactory.Create(builder => { builder.AddConsole(); });
然後,此單一/全域實例應該在 DbContextOptionsBuilder 上向 EF Core 註冊。 例如:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(MyLoggerFactory)
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFLogging;Trusted_Connection=True;ConnectRetryCount=0");
取得詳細訊息
小提示
當使用 AddDbContext 或將 DbContextOptions 實例傳遞至 DbContext 建構函式時,仍會呼叫 OnConfiguration。 這使其成為套用內容設定的理想位置,無論 DbContext 的建構方式為何。
敏感性資料
根據預設,EF Core 不會在例外狀況訊息中包含任何資料的值。 這是因為這類資料可能是機密的,而且如果未處理例外狀況,可能會在生產使用中洩漏。
不過,瞭解資料值 (尤其是索引鍵) 在偵錯時會非常有幫助。 您可以在 EF Core 中呼叫 EnableSensitiveDataLogging()來啟用。 例如:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.EnableSensitiveDataLogging();
詳細查詢例外狀況
基於效能考量,EF Core 不會將從資料庫提供者讀取值的每個呼叫包裝在 try-catch 區塊中。 不過,這有時會導致難以診斷的例外狀況,尤其是當資料庫在模型不允許時傳回 NULL 時。
開啟 EnableDetailedErrors 後,EF 會植入這些 try-catch 區塊,從而提供更詳細的錯誤資訊。 例如:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.EnableDetailedErrors();
特定訊息的設定
EF Core ConfigureWarnings API 可讓應用程式變更遇到特定事件時發生的情況。 這可用於:
- 變更記錄事件的記錄層級
- 完全不記錄事件
- 事件發生時拋出例外狀況
變更事件的日誌層次
有時,變更事件的預先定義日誌層級會很有用。 例如,這可用來將兩個額外的事件從 LogLevel.Debug 提升至 LogLevel.Information:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.ConfigureWarnings(
b => b.Log(
(RelationalEventId.ConnectionOpened, LogLevel.Information),
(RelationalEventId.ConnectionClosed, LogLevel.Information)));
禁止記錄事件
以類似的方式,可以禁止記錄個別事件。 這對於忽略已檢閱和瞭解的警告特別有用。 例如:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.ConfigureWarnings(b => b.Ignore(CoreEventId.DetachedLazyLoadingWarning));
為活動投擲
最後,EF Core 可以設定為針對指定事件擲回。 這對於將警告變更為錯誤特別有用。 (事實上,這就是方法的 ConfigureWarnings 最初目的,因此得名。比如:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.ConfigureWarnings(b => b.Throw(RelationalEventId.QueryPossibleUnintendedUseOfEqualsWarning));
篩選和其他設定
如需記錄篩選和其他設定的指引,請參閱 在 .NET 中記錄 。
EF Core 記錄事件定義在下列其中一項中:
- CoreEventId 適用於所有 EF Core 資料庫提供者通用的事件
- RelationalEventId 適用於所有關聯式資料庫提供者通用的事件
- 針對目前資料庫提供者的特定事件之類似類別。 例如, SqlServerEventId 針對 SQL Server 提供者。
這些定義包含每個事件的事件 ID、日誌層次和類別,正如 Microsoft.Extensions.Logging 所使用的一樣。