Skip to main content

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
OptionTypeDefaultDescription
PathstringrequiredFile path for logs
AsyncboolfalseNon-blocking writes with buffering
MaxFileSizeint10485760Max file size in bytes before rotation (10MB)
MaxRetainedFilesint5Number of rotated files to keep
CompressRotatedFilesboolfalseGzip old log files

Advanced file configuration:

{
"Type": "file",
"Path": "logs/app-.log",
"Async": true,
"MaxFileSize": 10485760,
"MaxRetainedFiles": 5,
"CompressRotatedFiles": true
}

Path patterns:

PatternResult
logs/app.logSingle file, rotated by size
logs/app-.logDaily 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