How to manage logging ?

Estimated reading: 3 minutes 321 views

  • EventId – Unique identifier of log entry.
  • ApplicationId – Unique identifier of the application for which the log entry was generated.
  • UserId – Repository ID of the current user.
  • TimeStamp – Date and time of the log entry message.
  • MachineName – Name of the computer.
  • Title – Title of the log entry message.
  • Message – Body of the log entry message.
  • Severity – Log entry severity as a TraceEventType enumeration

Audit (Event Log) operations

Add the following namespaces in your code

using Novalys.VisualGuard.Security;
using Novalys.VisualGuard.Security.Common;
using Novalys.VisualGuard.Security.CommonProfileAttribute;
using Novalys.VisualGuard.Security.Database;
using Novalys.VisualGuard.Security.Logging;
using Novalys.VisualGuard.Security.Membership;
using Novalys.VisualGuard.Security.UserProfile;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;

Define the Visual Guard runtime

VGSecurityRuntime runtime = VGSecurityManager.Runtime;

Get all Event Logs from the storage

var currentApp = runtime.Application.GetCurrentApplication();

//get all event logs, for an application
var logs = runtime.EventLogs.GetAllEventLogByApplication(currentApp);

//get all event logs, for an application, for a given date range
int totalLogs = 0;

DateTime startDate = DateTime.Now.Date.Subtract(new TimeSpan(30, 0, 0, 0));
DateTime endDate = DateTime.Now.Date;            

//get all event logs, for a particular user, for all applications, for a given date range
var logsbyUsername = runtime.EventLogs.GetAllEventLogByUsername("jsmith", startDate, endDate, 0, int.MaxValue, out totalLogs);

//Get all event logs, for all applications
var allEventLogs= runtime.EventLogs.GetAllEventLog();

//Get event logs by category
var logsByCategory = runtime.EventLogs.GetAllEventLogByCategory(VGLogCategory.AccountLogon, startDate, endDate);

Add your own Custom Event ID

//few examples for adding new custom event IDs 
runtime.EventLogs.AddCustomVGEventId(101, "Viewing employee");
runtime.EventLogs.AddCustomVGEventId(102, "Viewing customer");
runtime.EventLogs.AddCustomVGEventId(103, "Change employee salary");
runtime.EventLogs.AddCustomVGEventId(104, "Viewing sales order");
runtime.EventLogs.AddCustomVGEventId(105, "SalesOrder modified");
runtime.EventLogs.AddCustomVGEventId(106, "SalesOrder deleted");

Write an Event Log

//define properties
StringDictionary dictionary = new StringDictionary();
dictionary.Add("Id", "143");
dictionary.Add("EmployeeName", "Peter");

//define a log entry
var logentry_ViewEmployee = new VGLogEntry(101, "View Employee", "Consultation of Employee: [p:EmployeeName] [p:Id]", System.Diagnostics.TraceEventType.Information, dictionary);

//write log for an application

//define a log entry
dictionary.Add("OldSal", "5000");
dictionary.Add("NewSal", "6000");
var logentry_ChangeEmployeeSalary = new VGLogEntry(103, "Change employee salary", "Changed employee salary from [p:OldSal] to [p:NewSal], for employee: [p:EmployeeName] ", System.Diagnostics.TraceEventType.Information, dictionary);

//write log for an application

//you can also use the following methods for writing logs

Get all custom event IDs

 //Get all custom event Ids
var allCustomEventIds = runtime.EventLogs.GetAllCustomEventIds();

Delete a custom event ID

//find a particular Event ID
var findEventId = allCustomEventIds.Find(x => x.EventId == 106);

//Delete a custom Event ID

Clear logs

 //clear all logs, for the current application

//clear logs older than 15 days, for the current application 
runtime.EventLogs.ClearLog(currentApp,DateTime.Now.Date.Subtract(new TimeSpan(15,0,0,0)));