Click or drag to resize

Events Class

Represent an AskiaPortal log
Manage the events logs and activities.
Represent an AskiaPortal log
Inheritance Hierarchy
SystemObject
  AskiaPortalCmnEvents

Namespace:  AskiaPortalCmn
Assembly:  AskiaPortalCmn (in AskiaPortalCmn.dll) Version: 1.7.0-build068
Syntax
public static class Events

The Events type exposes the following members.

Properties
  NameDescription
Public propertyStatic memberActivity
Logs the user activity
Public propertyStatic memberSystem
Logs the system events
Top
Methods
  NameDescription
Public methodStatic memberCode exampleFind
Finds all occurrences in the database using the passed filters
Public methodStatic memberFindAllLogTags
Find all possible logs tags
Top
Remarks

The term `System` refer to the regular logs that could contains sensitive system information.
Example: "Execute SQL query on server with id 3 to create a new survey"

The term `Activity` refer to the user friendly logs and should not contains sensitive system information.
Example: "Creating a new survey"

Examples

Add new events

using System;
using AskiaPortalCmn.Events;

private static Guid TestLoggerGuid = Guid.Parse("590A6BE2-AA7E-49EF-8FBE-8C261C45E4F3");
private static Version TestLoggerVersion = new Version("1.0.0.0");

private static int Main(string[] args)
{
    Events.System.Log(null, "Starting the `testLogger` application", new List<string>{"start"});

    AskiaPortalCmnApi.OnSetup += AskiaPortalCmnApi_OnSetup;
    AskiaPortalCmnApi.InitializeModule(TestLoggerGuid, TestLoggerVersion);


    var context = ContextFactory.CreateForSuperAdmin();
    var result = Context.Validate();
    if (!result.Success)
    {
        Events.System.Error(null, result.Exception);
        return -1;
    }

    Events.System.Debug(context, "Doing some action...");
    // Do the action
    var code = DoAction();

    if (code != 0)
    {
        Events.Activity.Warn(context, $"Action done with the error code {code}");
    }
    else
    {
        Events.Activity.Info(context, "Action done successfully.");
    }
    return code;
}

private void AskiaPortalCmnApi_OnSetup(IModuleSetup moduleSetup, IModule module)
{
    Events.System.Info(null, $"Register the version `{moduleSetup.Version}` of the module `TestLogger`");
    moduleSetup.Name = "TestLogger";
    moduleSetup.Description = "Test logger";
    moduleSetup.Authors = "Askia SAS";
    moduleSetup.RootUrl = "/TestLogger";
    try
    {
        moduleSetup.Save();
    }
    catch (Exception ex)
    {
        Events.System.Fatal(null, ex);
    }
}

Read all events of the current user during the last minute -1

private static void PrintCurrentUserLastEvents(IContext context)
{
    Events.System.Info(context, $"Reading the last user logs");
    var filter = new EventsFilter
    {
        From = DateTime.UtcNow.AddMinutes(-2),
        To = DateTime.UtcNow.AddMinutes(-1),
        UserId = new List<int> { context.User.Id }
    };
    var logs = Events.FindAll(context, filter);
    foreach(var log in logs)
    {
        Console.WriteLine($"{log.Id} {log.CreatedAt} {log.Level} {log.Type} {log.Message}");
    }
}
See Also