๐ AuditModel Documentation (IrbyImaging.UI.Models)
๐ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Class Diagram (UML)
- Property Definitions & Key Functional Areas
- Integration Scenarios
- Sample Code Blocks & Usage Examples
- Error Handling & Return Types
- Third-Party Integrations
- Conclusion
1. Overview & Purpose
The AuditModel is a simple Plain Old CLR Object (POCO) that encapsulates all the information needed for tracking user activities within the IrbyImaging application. It serves as a data model to:
- ๐ต๏ธโโ๏ธ Record who did what, when, and from where.
- ๐ Feed audit logs, reports, or monitoring dashboards.
- ๐ Support security and compliance requirements by capturing login attempts, page accesses, session details, etc.
Namespace:
namespace IrbyImaging.UI.Models
2. Dependencies & Technologies
| Technology | Purpose |
|---|---|
| .NET Standard | Cross-platform class library support |
System |
Base namespace for primitive types (DateTime, Guid) |
| UI / ASP.NET Core | Typically used within Controllers, Filters, or Services |
Note: This model has no external NuGet dependenciesโjust the BCL (
System).
3. Class Diagram (UML)
classDiagram
class AuditModel {
+int AuditId
+string Area
+string ControllerName
+string ActionName
+Guid RoleId
+string LangId
+string IpAddress
+string IsFirstLogin
+DateTime ActivityDate
+string LoginStatus
+string PageAccessed
+string SessionId
+string UrlReferrer
+Guid UserId
+Guid? DocumentImageId
+string UserEmail
+string Namespace
}
Legend
โข + : public member
โข ? : nullable type
4. Property Definitions & Key Functional Areas
| Property | Type | Description |
|---|---|---|
| AuditId | int |
Auto-incremented primary key for the audit record. |
| Area | string |
MVC Area name or functional partition. |
| ControllerName | string |
Name of the controller where the action was invoked. |
| ActionName | string |
Name of the action method executed. |
| RoleId | Guid |
Identifier of the userโs role (e.g., Admin, User). |
| LangId | string |
Language or culture identifier (e.g., "en-US"). |
| IpAddress | string |
Clientโs IP address at time of access. |
| IsFirstLogin | string |
Flag (โtrueโ/โfalseโ) indicating first login status. |
| ActivityDate | DateTime |
Timestamp when the activity occurred. |
| LoginStatus | string |
Outcome of login attempt (e.g., โSuccessโ, โFailureโ). |
| PageAccessed | string |
Route URL or resource path accessed. |
| SessionId | string |
Unique session identifier. |
| UrlReferrer | string |
HTTP referrer header (previous page). |
| UserId | Guid |
Unique identifier of the user. |
| DocumentImageId | Guid? |
(Optional) Related document or image record. |
| UserEmail | string |
Email address of the user for quick reference. |
| Namespace | string |
CLR namespace where the audited code resides. |
5. Integration Scenarios
5.1 Audit Logging Service
- Controller / Filter intercepts an HTTP request.
- Build an
AuditModelinstance with context data. - Call
IAuditService.LogAsync(auditModel)to persist or push to an external system.
5.2 Database Persistence (Entity Framework Core)
- Map
AuditModelto anAudittable in your DbContext. - Use Fluent API or attributes to configure primary keys and indices on
ActivityDate,UserId, etc.
5.3 BI & Reporting
- Export audit logs to Power BI, ElasticSearch, or SQL Server Reporting Services.
- Slice by
Area,ActionName, orLoginStatusfor security analytics.
6. Sample Code Blocks & Usage Examples
6.1 Instantiating an AuditModel
var auditEntry = new AuditModel
{
AuditId = 0, // DB will assign if configured as identity
Area = "UserManagement",
ControllerName = "Account",
ActionName = "Login",
RoleId = currentUserRoleId,
LangId = Thread.CurrentThread.CurrentCulture.Name,
IpAddress = httpContext.Connection.RemoteIpAddress.ToString(),
IsFirstLogin = isFirstTime ? "true" : "false",
ActivityDate = DateTime.UtcNow,
LoginStatus = loginSuccess ? "Success" : "Failure",
PageAccessed = "/Account/Login",
SessionId = httpContext.Session.Id,
UrlReferrer = httpContext.Request.Headers["Referer"].ToString(),
UserId = currentUserId,
DocumentImageId = null,
UserEmail = currentUserEmail,
Namespace = typeof(AccountController).Namespace
};
6.2 Logging via an Audit Service
public interface IAuditService
{
Task LogAsync(AuditModel audit);
}
public class AuditService : IAuditService
{
private readonly ApplicationDbContext _db;
public AuditService(ApplicationDbContext db) => _db = db;
public async Task LogAsync(AuditModel audit)
{
_db.AuditLogs.Add(audit);
await _db.SaveChangesAsync();
}
}
// Usage in a Controller
await _auditService.LogAsync(auditEntry);
7. Error Handling & Return Types
AuditModel itself contains no methods, so error handling is delegated to the surrounding service or repository:
- Validation: Ensure required fields (e.g.,
UserId,ActivityDate) are not null. - Exception Handling: Wrap database or I/O calls in
try/catchand log exceptions separately.
8. Third-Party Integrations
While AuditModel is agnostic by design, it is often used alongside:
- ๐ Serilog, NLog, or log4net: For structured logging sinks.
- ๐ ElasticSearch/Kibana: For centralized audit search.
- ๐ Power BI: For visualizing audit trends.
9. Conclusion
The AuditModel is a versatile, lightweight data model that plays a crucial role in capturing and persisting user actions across the IrbyImaging application. By standardizing on this single POCO, you gain:
- โ Consistency in audit record shape
- ๐ Flexibility to plug into multiple storage or dashboard solutions
- ๐ Traceability for security, compliance, and troubleshooting
Use it as a building block in your audit pipelineโfrom ASP.NET Core filters to database ingestion and BI reporting.
Happy auditing! ๐