ILogger
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // ====================================================================== // Q: How do you log properly in .NET? // // ILogger<T> via DI, log levels per namespace in configuration, and never // passwords, tokens or personal data in logs. Export to Application Insights / // OpenTelemetry (Q33). // ====================================================================== // ✓ structured logging: OrderId / CustomerId become searchable fields logger.LogInformation("Order {OrderId} placed by {CustomerId}", order.Id, customerId); // ✗ interpolation: the fields are lost and the string is built even if the level is off logger.LogInformation($"Order {order.Id} placed by {customerId}"); // For hot paths: source-generated logging, without allocations [LoggerMessage(Level = LogLevel.Warning, Message = "ERP rejected {OrderNumber}: {Reason}")] static partial void LogErpRejected(ILogger logger, string orderNumber, string reason); |