TCIS.Logging.Autofac 1.0.7

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package TCIS.Logging.Autofac --version 1.0.7
                    
NuGet\Install-Package TCIS.Logging.Autofac -Version 1.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TCIS.Logging.Autofac" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TCIS.Logging.Autofac" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="TCIS.Logging.Autofac" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TCIS.Logging.Autofac --version 1.0.7
                    
#r "nuget: TCIS.Logging.Autofac, 1.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TCIS.Logging.Autofac@1.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TCIS.Logging.Autofac&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=TCIS.Logging.Autofac&version=1.0.7
                    
Install as a Cake Tool

TCIS.Logging.Autofac

A module supporting AOP (Aspect-Oriented Programming) for the logging system in TCIS, utilizing the Autofac library.

🌟 Overview

TCIS.Logging.Autofac replaces the default .NET DI Container with Autofac. Specifically, it provides:

  • Assembly Scanning: Automatically scans and registers Services by Suffixes.
  • AOP Interception: Automatically intercepts function calls to consistently log input parameters, execution time, and exceptions, without requiring repetitive logging code in the Business Logic.

📦 Installation

Install the package via .NET CLI:

dotnet add package TCIS.Logging.Autofac

⚙️ Configuration (appsettings.json)

You can customize AOP configurations (e.g., the list of suffixes to auto-register) in the TLogSettings:AopSettings section (default):

{
  "TLogSettings": {
    "AopSettings": {
      "DiRegistrationSuffixes": [ "Service", "Repository", "Manager" ]
    }
  }
}

Lọc field của data.result

TLogInterceptor là nơi duy nhất áp bộ lọc field của kết quả (ResultIncludeFields, ResultIncludeFieldsByMethod, ResultFilterMaxItems trong TLogSettings). Ví dụ:

{
  "TLogSettings": {
    "ResultIncludeFields": ["id", "code", "cd"],
    "ResultIncludeFieldsByMethod": { "OrderService.Get": ["id"] }
  }
}
  • Rỗng hoặc có "*" = ghi hết; khai báo toàn mục rỗng/gõ nhầm = không ghi result (fail-closed). Khoá ByMethod dùng tên lớp hiện thực (Class hoặc Class.Method, không phân biệt hoa thường).
  • Duyệt vét cạn mọi tầng object/list lồng nhau; field khớp được giữ nguyên cả giá trị; phần tử list không có field nào bị bỏ.
  • Che dữ liệu nhạy cảm luôn thắng; lọc chạy trước ResponseSize và ResponseSummary.
  • Quyết định lọc được cache theo phương thức của lớp hiện thực (chung nơi cache ExcludeClasses/ExcludeMethods).
  • Controller (ControllerLoggingFilter), gRPC và TLogWriter chưa lọc — chỉ AOP.

Chi tiết đầy đủ: README của TCIS.Logging.

Chẩn đoán treo (LogInvocationStart, SlowCallThreshold)

Log AOP (TLogInterceptor) chỉ ghi khi Proceed() trả về (trong finally). Một lời gọi treo — chờ DB, lock, HTTP không timeout — vì thế không để lại bản ghi nào. Hai tuỳ chọn trong TLogSettings bù chỗ này:

{
  "TLogSettings": {
    "LogInvocationStart": true,
    "InvocationStartLevel": "INFO",
    "SlowCallThreshold": "00:00:30"
  }
}
Khoá Mặc định Ý nghĩa
LogInvocationStart false Ghi bản ghi "{Class}.{Method} started" trước Proceed(): không có data, event.outcome: "unknown", cùng event.id với bản ghi kết thúc.
InvocationStartLevel "DEBUG" Mức của bản ghi started. Mặc định DEBUG sẽ bị admission loại khi min level là INFO (mặc định của lib) — hoặc hạ min level bằng Dynamic Log Level, hoặc đặt "INFO" khi cần điều tra mà không muốn hạ cả hệ thống.
SlowCallThreshold null (tắt) Lời gọi chạy quá ngưỡng mà chưa kết thúc ⇒ một bản ghi WARN "{Class}.{Method} still running after {ms}ms", event.outcome: "unknown", event.duration = ms đã trôi, cùng event.id với bản ghi kết thúc. <= 0 cũng là tắt.

Cơ chế và giới hạn:

  • Một sweeper dùng chung (một Timer, chu kỳ 1 giây, tạo lười) quét các lời gọi đang chạy — độ phân giải ~1 giây, không phải mili giây; không tạo Timer cho từng lời gọi. WARN bắn đúng một lần, không lặp theo bội số ngưỡng.
  • Ngữ cảnh (WorkContext, Activity) được chụp trên thread gọi lúc bắt đầu nên WARN vẫn mang đúng transaction/user dù sweeper chạy ở thread khác. WARN không bắn sau khi hàm đã hoàn tất; hàm xong trước ngưỡng thì không có WARN.
  • Hàm chạy lâu một cách hợp lệ (job, export) sẽ sinh WARN: loại chúng bằng ExcludeClasses/ExcludeMethods (đường bị loại không đăng ký sổ đang chạy). Khuyến nghị bật SlowCallThreshold cho API đồng bộ, không bật toàn cục mù quáng.
  • Cách đọc log: thấy WARN ... still running mà chưa có bản ghi kết thúc cùng event.id ⇒ lời gọi đã vào hàm và đang treo. Phạm vi: cả ba đường — hàm đồng bộ, Task, Task<T>; chỉ TLogInterceptor (controller/gRPC không có).
  • Giới hạn khi thread pool cạn: sweeper chạy trên thread pool. Đúng lúc thread pool bị bão hoà (thường đi kèm treo) callback có thể bị trễ hoặc không chạy, khi đó không có WARN — không có WARN chưa chứng minh là không treo.
  • event.id dùng chung cho started/WARN/kết thúc. Nếu pipeline phía sau (Filebeat/Logstash/ES ingest) đặt _id hoặc dedupe theo event.id, bản ghi kết thúc sẽ đè bản ghi treo. Thư viện không dùng event.id làm khoá chống trùng (dùng RecordId riêng cho spool/biên nhận). Đã kiểm trên Kibana UAT ngày 21/10/2026 (data view tcis-logs*, gồm các service eBerth): _id khác event.id ở mọi bản ghi lấy mẫu, nên ở đó không bị đè. Kết quả này phụ thuộc cấu hình pipeline của từng môi trường — môi trường khác (production, cụm khác) thì kiểm lại bằng cách so _id với event.id của một bản ghi trong Discover (tab JSON), hoặc bật thử một service rồi tìm theo event.id: thấy 2 bản ghi (started + kết thúc) là không bị đè.

Điều tra nhanh một service nghi bị treo ở Proceed()

Cấu hình đọc lúc khởi động (IOptions): đổi xong phải restart service đó. Mỗi service có appsettings riêng nên chỉ bật cho service đang nghi ngờ.

{
  "TLogSettings": {
    "SlowCallThreshold": "00:00:10"
  }
}
  1. Bước 1 — chỉ cần SlowCallThreshold (ghi WARN nên không phải hạ min level, chi phí thấp). Thấy WARN ... still running mà chưa có bản ghi kết thúc cùng event.id ⇒ lời gọi đã vào hàm và đang treo; kết luận "API chưa được gọi" là sai. Hàm chạy lâu hợp lệ gây ồn thì thêm vào ExcludeMethods/ExcludeClasses.
  2. Bước 2 — nếu cần biết mọi lời gọi có vào hàm hay không, thêm "LogInvocationStart": true và "InvocationStartLevel": "INFO" (hoặc giữ DEBUG và hạ min level bằng Dynamic Log Level). Nặng hơn: mỗi lời gọi được log thêm một bản ghi, chỉ bật trong lúc điều tra rồi tắt.
  3. Điều tra xong: bỏ hai khoá trên và restart.

🚀 Usage

In Program.cs, configure the IHostBuilder to use UseTAutofacLogging:

using Microsoft.Extensions.Hosting;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

// Configure Autofac as the main DI Container and configure AOP Logging
builder.Host.UseTAutofacLogging(
    scanAssemblies: new[] { Assembly.GetExecutingAssembly() }, // Scan the current Assembly
    configSection: "TLogSettings:AopSettings",                 // Configuration section
    configureOptions: options => 
    {
        // Can be configured via code instead of appsettings
        options.DiRegistrationSuffixes.Add("Facade");
    }
);

var app = builder.Build();
app.Run();

💡 Basic Example

When you have an Interface and an implementation Class with a name ending in Service (e.g., IUserService and UserService):

public interface IUserService 
{
    Task<User> GetUserAsync(int id);
}

// This class will be automatically registered by Autofac (due to the 'Service' suffix)
// and automatically intercepted to log Input, Output, and Exceptions.
public class UserService : IUserService
{
    public async Task<User> GetUserAsync(int id)
    {
        return await _db.Users.FindAsync(id);
    }
}

By simply configuring UseTAutofacLogging(), you no longer need to manually register builder.Services.AddScoped<IUserService, UserService>().

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on TCIS.Logging.Autofac:

Package Downloads
TCIS.Pluggable.Engine.Autofac

TCIS Core Framework is an application framework for building modular, multi-tenant applications on ASP.NET Core. Autofac integration for TCIS.Pluggable.Engine

TCIS.Logging.Hosting

One-call TCIS application logging composition, startup lifecycle and downstream failure attribution.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.8-rc.5 49 10/23/2026
1.0.8-rc.4 45 10/23/2026
1.0.8-rc.3 45 10/23/2026
1.0.8-rc.2 41 10/22/2026
1.0.8-rc.1 52 10/22/2026
1.0.7 76 10/22/2026
1.0.6 115 10/20/2026
1.0.6-rc.1 49 10/20/2026
1.0.5 127 10/19/2026
1.0.4 114 10/13/2026
1.0.3 110 10/13/2026
1.0.2 117 10/12/2026
1.0.2-rc.1 62 10/12/2026
1.0.1-rc.10 99 10/8/2026
1.0.1-rc.9 67 10/8/2026
1.0.1-rc.8 80 10/8/2026
1.0.1-rc.7 72 10/8/2026
1.0.1-rc.6 79 10/8/2026
1.0.1-rc.4 67 10/7/2026
1.0.1-rc.3 69 10/7/2026
Loading failed