How manage logging ?

Estimated reading: 3 minutes 209 views

  • EventId – The unique identifier of log entry.
  • ApplicationId – The unique identifier of the application for which the log entry was generated.
  • UserId – The repository id of the current user.
  • TimeStamp – Date and time of the log entry message.
  • MachineName – The Name of the computer.
  • Title – The title of the log entry message.
  • Message – The body of the log entry message.
  • Severity – Log entry severity as a TraceEventType enumeration

Audit(EventLog) operations

Add 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 VisualGuard runtime

VGSecurityRuntime runtime = VGSecurityManager.Runtime;

Get all eventlogs from storage

var currentApp = runtime.Application.GetCurrentApplication();

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

//get all eventlogs for application for 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 eventlogs for particular user for all applications for given date range
var logsbyUsername = runtime.EventLogs.GetAllEventLogByUsername("jsmith", startDate, endDate, 0, int.MaxValue, out totalLogs);

//Get all eventlogs for all applications
var allEventLogs= runtime.EventLogs.GetAllEventLog();

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

Add your own Custom EventId

//few examples for adding new custom eventIds 
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 logentry
var logentry_ViewEmployee = new VGLogEntry(101, "View Employee", "Consultation of Employee: [p:EmployeeName] [p:Id]", System.Diagnostics.TraceEventType.Information, dictionary);

//write log for application

//define logentry
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 application

//you can also use following methods for writing log

Get all custom eventIds

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

Delete custom eventId

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

//Delete customEventId

Clear logs

 //clears all logs for current application

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

Leave a Comment

Share this Doc