Console
Step 1: Installing the Package
Install the PrimusSaaS.Logging package via NuGet:
dotnet add package PrimusSaaS.Logging --version 1.2.6
Step 2: Configuring Program.cs
Register Primus Logging in your Program.cs:
using PrimusSaaS.Logging.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Logging.ClearProviders();
builder.Logging.AddPrimus(builder.Configuration.GetSection("PrimusLogging"));
var app = builder.Build();
app.UsePrimusLogging();
app.MapControllers();
app.Run();
Step 3: Configuring App Settings
Configure the Console target in your appsettings.json:
{
"PrimusLogging": {
"ApplicationId": "MyService",
"Environment": "Development",
"MinLevel": 1,
"Targets": [
{
"Type": "console",
"Pretty": true
}
]
}
}
Configuration options explained
| Option | Description |
|---|---|
ApplicationId | Name of your service (appears in logs) |
Environment | Deployment environment (Development, Production) |
MinLevel | Minimum log level: 0=Debug, 1=Info, 2=Warning, 3=Error |
Pretty | Enable colored, formatted output |
Step 4: Log Something
Create a service that logs messages and register it in Program.cs:
// In Program.cs — register the service BEFORE builder.Build()
builder.Services.AddScoped<OrderService>();
// OrderService.cs
public class OrderService
{
private readonly ILogger<OrderService> _logger;
public OrderService(ILogger<OrderService> logger)
{
_logger = logger;
}
public void ProcessOrder(string orderId, string customerEmail)
{
_logger.LogInformation("Processing order {OrderId} for {CustomerEmail}", orderId, customerEmail);
_logger.LogInformation("Order {OrderId} completed successfully", orderId);
}
}
Don't forget
builder.Services.AddScoped<OrderService>()— without it, the service won't be injected and no logs will appear.
Step 5: Verify Console Output
Run your application and trigger some logs. With Pretty: true, each log entry produces two lines — this is intentional:
Line 1 — colored human-readable summary:
[05:58:24 AM] INFO: Processing order ORD-12345 for u***@***.com
Line 2 — structured JSON context block (dark gray):
{
"applicationId": "MyService",
"correlationId": "corr-54443894bdcd4b729cc75a5df7e3530f",
"RequestPath": "/orders",
"durationMs": 7.356
}
The JSON block contains all the structured context fields enriched by Primus — correlation IDs, request paths, timing, and any custom properties you logged. This is the data that gets forwarded to file and Application Insights targets.
Want a single line per entry? Set
"Pretty": false— logs will output as compact JSON objects, one per line. This is the recommended setting for production/container environments where log aggregators parse stdout.
Notice:
- Colored log levels - INFO in green, WARNING in yellow, ERROR in red
- PII masked - Email appears as
u***@***.com(if PII masking enabled) - Correlation ID - Same ID groups related logs from one request together
What does app.UsePrimusLogging() do?
UsePrimusLogging() registers middleware that runs on every HTTP request. It automatically:
- Generates a Correlation ID per request (or reads it from the
X-Correlation-Idheader if already present) - Attaches the Correlation ID to all logs made during that request
- Logs the incoming request and outgoing response (method, path, status code, duration)
Is it required? No, but without it you lose automatic Correlation ID tracking and request/response logging. For production apps, always include it.
Troubleshooting
dotnet add package PrimusSaaS.Logging fails — package not found
The package may be on a private NuGet feed. Ask your team admin for the feed URL, then:
dotnet nuget add source https://your-feed-url/v3/index.json --name PrimusNuGet
dotnet add package PrimusSaaS.Logging --version 1.2.6
Logs are not appearing
Check that MinLevel is set low enough. A value of 1 (Info) suppresses Debug (0) logs. Set to 0 temporarily to see all output.
AddPrimus() — which overload should I use?
All three are valid:
// Option A — config-section binding (recommended, matches appsettings.json)
builder.Logging.AddPrimus(builder.Configuration.GetSection("PrimusLogging"));
// Option B — code-based lambda configuration
builder.Logging.AddPrimus(opts => builder.Configuration.GetSection("PrimusLogging").Bind(opts));
// Option C — defaults only (no appsettings needed, for quick tests)
builder.Logging.AddPrimus();
app.UsePrimusLogging() throws InvalidOperationException
Ensure builder.Logging.AddPrimus(...) is called before builder.Build(). UsePrimusLogging() must be called after var app = builder.Build().
PII masking is not working
PII masking is on by default. If you see unmasked data, check that "EnablePiiMasking": false is not set in your PrimusLogging config section.
Colored output not showing in CI/Docker
Set "Pretty": false in the console target config. ANSI color codes don't render in most CI environments.