Library for structured logging in PostgreSQL, providing an isolated and configurable audit log context for applications.
$ dotnet add package Flavio.Santos.DbLogger.PostgreSQL🚀 FDS.DBLogger.PostgreSQL is a robust .NET library for structured audit logging in PostgreSQL, designed to track and log application events efficiently.
You can install this package via NuGet Package Manager:
dotnet add package Flavio.Santos.DBLogger.PostgreSQL --version 1.0.6
Or using Package Manager Console:
Install-Package Flavio.Santos.DBLogger.PostgreSQL -Version 1.0.6
The audit_logs table stores all audit log entries in a structured format in a PostgreSQL database.
audit_logsCREATE TABLE audit_logs (
id UUID PRIMARY KEY,
event_timestamp_local TIMESTAMP WITHOUT TIME ZONE,
event_timestamp_utc TIMESTAMP WITH TIME ZONE,
event_action VARCHAR(255) NOT NULL,
context_name VARCHAR(255) NOT NULL,
trace_identifier VARCHAR(50),
http_method VARCHAR(10) NOT NULL,
request_path VARCHAR(500),
http_status_code INTEGER,
event_message TEXT,
request_data JSONB,
response_data JSONB,
user_id UUID
);
CREATE INDEX idx_audit_logs_user_id ON audit_logs (user_id);
You can register the audit logger using a connection string from your configuration files (e.g., appsettings.json) and provide a fallback value:
using FDS.DbLogger.PostgreSQL.Published;
var auditLogConnectionString = configuration.GetConnectionString("AuditLogConnection")
?? "Host=localhost;Database=AuditLogDb;Username=defaultUser;Password=defaultPass";
services.AddDbLogger(auditLogConnectionString);Use LogCreateAsync() to log the creation of an entity, capturing relevant request and response data.
ClientServiceusing FDS.DbLogger.PostgreSQL.Published;
public async Task<Response<ClientDto>> AddAsync(ClientRequestDto request)
{
string requestId = string.Empty;
string msg = string.Empty;
try
{
requestId = await _auditLogService.LogInfoAsync("[START] - Client creation process started.", request);
msg = await _clientRepository.ExistsByNameAsync(request.Name);
if (!string.IsNullOrEmpty(msg))
{
await _auditLogService.LogValidationErrorAsync(msg, request);
return Result.CreateValidationError<ClientDto>(msg);
}
var client = new Client(request.Name);
msg = await _clientRepository.AddAsync(client);
var clientDto = new ClientDto { Id = client.Id, Name = client.Name };
await _auditLogService.LogCreateAsync(msg, request, clientDto);
return Result.CreateAdd(msg, clientDto);
}
catch (Exception ex)
{
msg = $"An unexpected error occurred: {ex.Message}";
await _auditLogService.LogErrorAsync(msg);
return Result.CreateError<ClientDto>(msg);
}
finally
{
msg = "Client creation process completed.";
await _auditLogService.LogEndAsync(msg);
RequestDataStorage.ClearData(requestId);
}
}CREATE, DELETE, UPDATE, ERROR, etc.).Entity Framework Core.LogCreateAsync() to avoid blocking API execution.This project is licensed under the MIT License - see the LICENSE file for details.