Skip to content

A simple framework to audit entities in .NET and .NET Core...

License

Notifications You must be signed in to change notification settings

laget-se/laget.Auditing

Repository files navigation

laget.Auditing

A simple framework to audit entities in .NET and .NET Core...

Nuget Nuget

Usage

By

public class By
{
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("ip")]
    public string Ip { get; set; }
    [JsonProperty("referenceId")]
    public string ReferenceId { get; set; }
    [JsonProperty("superadmin")]
    public bool Superadmin { get; set; } = false;
}

Auditor

Events (built-in)

Activated Added Archived Archived Associated Attached
Cleaned Cloned Connected Created Deactivated Deleted
Descheduled Detached Disassociated Disconnected Enqueued Failed
Followed Information Inserted Migrated Published Purged
Removed Reported Restored Scheduled Sent Succeded
Unfollowed Unpublished Updated

.With(Expression<Func<IEvent, object>> expression, object value)

Property Type Default Description
ClubId int 0 Should be set if the event was triggered from the context of a club
SiteId int 0 Should be set if the event was triggered from the context of a site
System string Calling assembly Indicates what system the event was triggered from, is
Reference object null Indicates what object the event refers to, e.g. if you add an attribute for a user the attribute object should be set as the reference
By By (class) null Indicates what user, if any, triggered the event

Examples

public class AccountService
{
    private readonly IAuditor _auditor;
    private readonly IAccountRepository _repository;

    public AccountService(IAccountRepository repository)
    {
        _auditor = new Auditor("ServiceBusConnectionString");
        _repository = repository;
    }


    public Create(Account account, By by)
    {
        account = _repository.Create(account, By by);

        var message = new Created(nameof(Account), account)
            .With(x => x.By, by);

        _auditor.Send(message);
    }

    public Update(Account account, By by)
    {
        _repository.Update(account);

        var message = new Updated((nameof(Account), account)
            .With(x => x.By, by);

        _auditor.Send(message);
    }

    public Delete(Account account, By by)
    {
        _repository.Delete(account);

        var message = new Deleted((nameof(Account), account)
            .With(x => x.By, by);

        _auditor.Send(message);
    }

    public Remove(Account account, Site site, By by)
    {
        _repository.Delete(account);

        var message = new Remove((nameof(Account), account)
            .With(x => x.By, by)
            .With(x => x.Reference, site);

        _auditor.Send(message);
    }
}
public class AccountService
{
    private readonly IAuditor _auditor;
    private readonly IAccountRepository _repository;

    public AccountService(IAccountRepository repository)
    {
        _auditor = new Auditor("ServiceBusConnectionString");
        _repository = repository;
    }


    public Create(Account account, By by)
    {
        var account = new
        {
            id = 123,
            name = "Jane Doe"
        };
        var site = new
        {
            id = 123,
            name = "FC GonAce"
        };

        var message = new Created("Account", account)
            .With(x => x.By, by)
            .With(x => x.Reference, site);

        await _auditor.Send(message);
    }
}

About

A simple framework to audit entities in .NET and .NET Core...

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •  

Languages