User Identification
User identification allows you to associate logs with specific users, making it easier to track user actions, debug user-specific issues, and maintain audit trails.
Basic Usage
Use Log.Identify()
to associate logs with a specific user:
// Identify user
Log.Identify(new AuthUser
{
Id = "12345",
Email = "user@example.com"
});
// All subsequent logs will be associated with this user
Log.Info("User performed action");
Log.Error("User encountered error", errorContext, ex);
// Logout to make subsequent logs anonymous
Log.Logout();
AuthUser Model
The AuthUser
class contains user identification information:
namespace Bytehide.Logger.Common.Models.Users;
public class AuthUser
{
public string Id { get; set; }
public string Email { get; set; }
public string Token { get; set; }
}
User Identification Methods
Identify User
// Basic user identification
Log.Identify(new AuthUser
{
Id = "user_12345",
Email = "john.doe@company.com"
});
// All subsequent logs will be associated with this user
Log.Info("User logged in successfully");
Log.Debug("User accessing dashboard");
Update Previous Logs
// Associate previous anonymous logs with the user
Log.Identify(new AuthUser
{
Id = "user_12345",
Email = "john.doe@company.com"
}, updatePreviousLogs: true);
// This will update anonymous logs sent before identification
User Logout
// Make subsequent logs anonymous
Log.Logout();
// All logs after logout will be anonymous
Log.Info("Anonymous action performed");
Integration Example
ASP.NET Core Login/Logout
[ApiController]
public class AuthController : ControllerBase
{
[HttpPost("login")]
public async Task<IActionResult> Login(LoginRequest request)
{
var user = await _authService.AuthenticateAsync(request.Email, request.Password);
// Identify user in logs
Log.Identify(new AuthUser
{
Id = user.Id,
Email = user.Email,
Token = user.AuthToken
});
Log.Info("User login successful");
return Ok(new { Token = user.AuthToken });
}
[HttpPost("logout")]
public IActionResult Logout()
{
Log.Logout();
return Ok();
}
}
updatePreviousLogs Parameter
When set to true
, associates previous anonymous logs with the user:
// Anonymous logs
Log.Info("Application started");
Log.Info("Loading configuration");
// Identify user and update previous logs
Log.Identify(new AuthUser
{
Id = "user_123",
Email = "john@example.com"
}, updatePreviousLogs: true);
// Previous logs are now associated with user_123
Best Practices
User Identification Best Practices
- Set user context early: Configure user identification as soon as user is authenticated
- Include relevant properties: Add role, department, or other contextual information
- Update on user changes: Refresh user context when user switches or logs out
- Use consistent user IDs: Maintain the same user ID format across your application
- Consider privacy: Be mindful of PII in user identification data
Next Steps
- Correlation IDs - Track requests across services
- Global Properties - Add consistent context to all logs
- Data Masking - Protect sensitive user information