๐Ÿ“‹ AuditModel Documentation (IrbyImaging.UI.Models)


๐Ÿ“ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class Diagram (UML)
  4. Property Definitions & Key Functional Areas
  5. Integration Scenarios
  6. Sample Code Blocks & Usage Examples
  7. Error Handling & Return Types
  8. Third-Party Integrations
  9. 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

  1. Controller / Filter intercepts an HTTP request.
  2. Build an AuditModel instance with context data.
  3. Call IAuditService.LogAsync(auditModel) to persist or push to an external system.

5.2 Database Persistence (Entity Framework Core)

  • Map AuditModel to an Audit table 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, or LoginStatus for 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/catch and 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! ๐Ÿš€