File Output
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);
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 File target in your appsettings.json:
{
"PrimusLogging": {
"ApplicationId": "MyService",
"Environment": "Production",
"MinLevel": 1,
"Targets": [
{
"Type": "file",
"Path": "logs/app.log"
}
]
}
}
File target options explained
| Option | Type | Default | Description |
|---|---|---|---|
Path | string | required | File path for logs |
Async | bool | false | Non-blocking writes with buffering |
MaxFileSize | int | 10485760 | Max file size in bytes before rotation (10MB) |
MaxRetainedFiles | int | 5 | Number of rotated files to keep |
CompressRotatedFiles | bool | false | Gzip old log files |
Advanced file configuration:
{
"Type": "file",
"Path": "logs/app-.log",
"Async": true,
"MaxFileSize": 10485760,
"MaxRetainedFiles": 5,
"CompressRotatedFiles": true
}
Path patterns:
| Pattern | Result |
|---|---|
logs/app.log | Single file, rotated by size |
logs/app-.log | Daily rolling: app-2024-01-15.log |
Step 4: Log Something
Create a service that logs messages:
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);
// Simulate processing
_logger.LogInformation("Order {OrderId} completed successfully", orderId);
}
}
Step 5: Verify Log File
After running your application and generating some logs, check the log file:
File location: logs/app.log (relative to your application's working directory)
Sample content:
{"Timestamp":"2024-01-15T10:30:45","Level":"Information","Message":"Processing order ORD-12345 for u***@***.com","Category":"OrderService","CorrelationId":"abc-123-def","ApplicationId":"MyService","Properties":{"OrderId":"ORD-12345","CustomerEmail":"u***@***.com"}}
{"Timestamp":"2024-01-15T10:30:45","Level":"Information","Message":"Order ORD-12345 completed successfully","Category":"OrderService","CorrelationId":"abc-123-def","ApplicationId":"MyService","Properties":{"OrderId":"ORD-12345"}}
Notice:
- Structured JSON - Each log is a single line of JSON
- PII masked - Email appears as
u***@***.com(if PII masking enabled) - Correlation ID - Same ID links related logs together
- Properties - Structured data is preserved for querying