Table of Contents

Interface ILogHelper

Namespace
Skyline.DataMiner.Net.LogHelpers
Assembly
SLNetTypes.dll

Helper interface for logger in Elasticsearch.

public interface ILogHelper
Extension Methods

Examples

	public class Script
{
        private ILogHelper _log;
        private Engine _engine;
        private ResourceManagerHelper _rmHelper;

        public void Run(Engine engine)
       {
           _engine = engine;
            _rmHelper = new ResourceManagerHelper();
           _rmHelper.RequestResponseEvent += (sender, args) => args.responseMessage = _engine.SendSLNetSingleResponseMessage(args.requestMessage);

           // This call retrieves a connection object for the user running the script.
           // This ensures the lines are logged under the correct user.
           IConnection userConnection = engine.GetUserConnection();

           // In a QAction, you can use the GetUserConnection() from the SLProtocol interface.
           // Create the log helper. Make sure it is always disposed!
           using (_log = LogHelper.Create(userConnection, "SRM"))
           {
               // This is logged under Category "Scripting.SRM" on LogLevel.Info.
               _log.Log.Info($"Starting booking validation.");

               Guid reservationId = Guid.NewGuid();
               ValidateBooking(reservationId);

               // Log lines are not instantly readable.
               _engine.Sleep(5000);

               ExampleReadLogLines(reservationId);
           }
       }

       private void ValidateBooking(Guid reservationId)
       {
            // The logger will now log under category "Scripting.ReservationInstanceID_c79d0529-6744-4fff-b102-2c62c9d30ebf"
           _log.ChangeSubjectId(new ReservationInstanceID(reservationId));

           // By default, the log helper will only log Info and above (Info, Warning, Error, Fatal).
           // If needed, this can be changed. See examples below:
           // This enables logging for all levels, except for Trace (Debug, Info, Warning, Error, Fatal):
           // _log.ChangeLogLevel(LogLevel.Debug);

           // This disables logging for all levels, except for Error and Fatal (Trace, Debug, Info)
           // _log.ChangeLogLevel(LogLevel.Error);
           _log.Log.Trace($"Starting Get Reservation Instance");

           ReservationInstance reservation = _rmHelper.GetReservationInstance(reservationId);

           _log.Log.Trace($"Ended Get Reservation Instance");

           if (reservation == null)
           {
               _log.Log.Error($"Could not find the reservation instance.");
           }
           else
           {
               // By default the log level is "Info" which means this Debug logging will not be stored in the database
               _log.Log.Debug("Retrieved with name {0} and with status {1}", reservation.Name, reservation.Status);
           }
       }

       private void ExampleReadLogLines(Guid reservationId)
       {
           ReservationInstanceID resId = new ReservationInstanceID(reservationId);

           // Get all logs for a specific reservation.
           // Reading using implicit paging:

           List<LogEntry> logs = _log.LogEntries.Read(LogEntry.Exposers.Name.Equal(resId.ToFileFriendlyString()).ToQuery());

           foreach (LogEntry log in logs)
           {
                // Outputs: "(Implicit Paging) Found log: Could not find the reservation instance. at 6/23/2020 2:02:39 PM +02:00 by SKYLINE2\SvenDD on 668 for ReservationInstanceID_fa3317a7-e7be-4b95-9ae2-56e407c55d2d within Scripting"
               _engine.GenerateInformation($"(Implicit Paging) Found log: {log.Message} at {log.TimeStamp} by {log.UserName} on {log.DataMinerId} for {log.Name} within {log.Category}");
           }

           // Reading using explicit paging
           PagingHelper<LogEntry> pagingHelper =
           _log.LogEntries.PreparePaging(LogEntry.Exposers.Name.Equal(resId.ToFileFriendlyString()).ToQuery(), 100);

           while (pagingHelper.MoveToNextPage())
           {
               List<LogEntry> currentPage = pagingHelper.GetCurrentPage();
               foreach (LogEntry log in currentPage)
               {
                   // Outputs: "(Explicit Paged) Found log: Could not find the reservation instance. at 6/23/2020 2:02:39 PM +02:00 by SKYLINE2\SvenDD on 668 for ReservationInstanceID_fa3317a7-e7be-4b95-9ae2-56e407c55d2d within Scripting"
                   _engine.GenerateInformation($"(Explicit Paged) Found log: {log.Message} at {log.TimeStamp} by {log.UserName} on {log.DataMinerId} for {log.Name} within {log.Category}");
               }
           }

           // Or reading using the Repository API
           // Use IDataSetRepository<LogEntry> repository = _log.LogEntries.RawRepository;
           // Getting all logs from today ordered by time
           DateTimeOffset now = DateTimeOffset.UtcNow;

           DateTimeOffset midNight = new DateTimeOffset(now.Year, now.Month, now.Day, 0, 0, 0, TimeSpan.Zero);

           IQuery<LogEntry> filter = LogEntry.Exposers.Category.Equal(_log.Category.ToString())
           .AND(LogEntry.Exposers.TimeStamp.GreaterThanOrEqual(midNight)).ToQuery()
           .OrderBy(LogEntry.Exposers.TimeStamp);

           List<LogEntry> scriptingLogs = _log.LogEntries.Read(filter);

           foreach (LogEntry log in scriptingLogs)
           {
               // Outputs:
               //  - "(All Scripting Today) Found log: Starting booking validation. at 6/23/2020 2:00:03 PM +02:00 by SKYLINE2\SvenDD on 668 for SRM within Scripting"
               //  - "(All Scripting Today) Found log: Could not find the reservation instance. at 6/23/2020 2:00:03 PM +02:00 by SKYLINE2\SvenDD on 668 for ReservationInstanceID_e2b57e85-84a4-4d55-a15f-2b01b5070fd1 within Scripting"
               _engine.GenerateInformation($"(All Scripting Today) Found log: {log.Message} at {log.TimeStamp} by {log.UserName} on {log.DataMinerId} for {log.Name} within {log.Category}");
       }
   }
}

Remarks

Available from DataMiner 10.0.10 onwards (RN 26434).

Properties

Category

Gets the logger category.

FlushInterval

Gets the interval at which new LogEntry objects are flushed to the database.

FlushToDatabaseAfterUpsert

Gets a value indicating whether LogEntries are flushed to database after upserting.

Log

Gets the logger instance

LogEntries

Gets the log entries CRUD helper component.

LogLevel

Gets the log level.

MaxLogEntriesBeforeFlush

Gets the max amount of LogEntries to keep in memory before doing a flush to database.

SubjectID

Gets the subject ID.

SubjectName

Gets the subject name.

Methods

ChangeFlushInterval(TimeSpan)

Changes the flush interval to the specific flush interval.

ChangeFlushToDatabaseAfterUpsert(bool)

Changes the flush to database after upsert configuration.

ChangeLogLevel(LogLevel)

Changes the log level to the specified log level.

ChangeMaxLogEntriesBeforeFlush(int)

Changes the maximum number of log entries before flush to the specified number.

ChangeSubjectId(IDMAObjectRef)

Changes the subject ID.

ChangeSubjectName(string)

Changes the subject name to the specified subject name.