Application Insights
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 Application Insights target in your appsettings.json:
{
"PrimusLogging": {
"ApplicationId": "MyService",
"Environment": "Production",
"MinLevel": 1,
"Targets": [
{
"Type": "applicationInsights",
"ConnectionString": "InstrumentationKey=your-key-here;IngestionEndpoint=https://your-region.in.applicationinsights.azure.com/"
}
]
}
}
How to create Application Insights resource and get Connection String
1. Create an Application Insights Resource
- Go to Azure Portal and sign in
- Click + Create a resource in the top left
- Search for Application Insights and select it
- Click Create
- Fill in the required fields:
- Subscription: Select your Azure subscription
- Resource Group: Create new or select existing (e.g.,
my-app-rg) - Name: Enter a name (e.g.,
my-app-insights) - Region: Choose your preferred region
- Resource Mode: Select Workspace-based (recommended)
- Log Analytics Workspace: Create new or select existing
- Click Review + create, then Create
- Wait for deployment to complete (usually 1-2 minutes)
2. Get the Connection String
- Once created, click Go to resource
- In the Overview page, find Connection String
- Click the Copy button next to it
The connection string looks like:
InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;IngestionEndpoint=https://eastus-1.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/
3. Store Securely
For production, store the connection string in:
- Azure Key Vault (recommended)
- Environment variables
- .NET User Secrets (for development)
# Using .NET User Secrets
dotnet user-secrets set "PrimusLogging:Targets:0:ConnectionString" "your-connection-string"
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 Logs in Azure Portal
After running your application and generating some logs:
- Go to your Application Insights resource in Azure Portal
- Click Logs in the left menu
- Run this query to see your logs:
traces
| where cloud_RoleName == "MyService"
| order by timestamp desc
| take 50
You should see your log messages with:
- Timestamp: When the log was created
- message: Your log message (with PII masked if configured)
- customDimensions: Contains
OrderId,CorrelationId, etc.
Verify PII Masking
If you enabled PII masking, emails will appear as u***@***.com instead of the actual email address.
Troubleshooting
| Issue | Solution |
|---|---|
| No logs appearing | Wait 2-5 minutes (ingestion delay) |
| Connection refused | Check connection string is correct |
| Logs missing fields | Ensure UsePrimusLogging() middleware is added |