Logging and History Methods
Overview
Lists available options and methods for logging and debugging using ExportManager.Details
Logging is achieved using the NLog NuGet package. Setting up, managing, and using logging is a very important part of developing exports in ExportManager. If you miss a step or forget something, the program does its best to detect what is wrong and tell you about it in the logs.
ExportManager also provides a HistoryEntry class which records various metadata about the current execution of an export and saves it to WarehouseDB.exports.dbo.ExportManagerRunHistory.
Setup
All logging configuration is set up in the NLog.config XML file located in the ExportManager program instance root. Default configuration varies by account, however all log files are written to the log subdirectory of the ExportManager instance root and named with the same name as your export class. Log files roll over daily. The main program itself keeps logs in ExportManager.log.
Logs can be configured to email log messages to certain recipients based on severity and source class.
A Logger object is automatically set up for you to use in your export under the variable name log.
You also have access to local variables representing the current history entry and the previous history entry that ran with the same arguments:
/// <summary> /// The history entry for this execution of the export /// </summary> protected HistoryEntry historyEntry; /// <summary> /// Gets the HistoryEntry object representing the last run of this export with the same arguments. /// </summary> protected HistoryEntry lastRun;
Execution
The log object contains many methods to help you write log messages. Click here for basic usage.
The HistoryEntry objects contain these properties which are automatically maintained for the most part, if you use all the built-in methods for the various steps of your export.
public string ExportName public string Arguments public DateTime StartTime public DateTime EndTime public int Result public long DataSize public int ColleagueProcesses public int SqlResultSets public int FilesWritten public int FilesImported public int FilesExported public int FilesUploaded public int FilesFailed public byte[] ScriptHash public static IEnumerable<string> Fields public bool Exists
At the end of your script, just prior to calling ArchiveFiles(), you must call SaveHistory() if you wish to save the current historyEntry to the database.
Example
Script
This example writes several log messages.
log.Info( "We have started the program." ); log.Debug( "The current date is {0}", DateTime.Now.ToString() ); log.Error( new Exception( "An exception has been thrown." ) ); SaveHistory(); ArchiveFiles( daysToKeep: 7, filesToKeep: 0 );
Log output
Expected <MyExport>.log output:
2016-09-14 11:28:08.1598 INFO ------------------------------------------------------------------------------- 2016-09-14 11:28:08.1668 INFO IWU.ExportManager.Exports.FileSample initializing. 2016-09-14 11:28:08.1668 INFO ------------------------------------------------------------------------------- 2016-09-14 11:28:08.7788 WARN TEST MODE is enabled. No actual data files will be exported/uploaded. Instead, write access will be tested. Override with "notest". 2016-09-14 11:28:08.7998 DEBUG Data directory: C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\data\FileSample 2016-09-14 11:28:08.8068 DEBUG Archive directory: C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\archive\FileSample 2016-09-14 11:28:08.8068 INFO We have started the program. 2016-09-14 11:28:08.8338 DEBUG The current date is 9/14/2016 11:28:08 AM 2016-09-14 11:28:08.8338 ERROR System.Exception: An exception has been thrown. 2017-11-03 10:48:30.0494 TRACE >>>> Entering "SaveHistory" with arguments ( ) 2017-11-03 10:48:30.0650 INFO Writing history entry to [exports].[ExportManagerRunHistory] 2017-11-03 10:48:30.0806 TRACE { ExportName = "MyExport", Arguments = "do_demo", StartTime = "11/3/2017 10:48:21 AM", EndTime = "11/3/2017 10:48:30 AM", Result = "0", DataSize = "13823772", ColleagueProcesses = "0", SqlResultSets = "8", FilesWritten = "8", FilesImported = "0", FilesExported = "0", FilesUploaded = "0", FilesFailed = "0", ScriptHash = "AD4960A97C21E3BB5F6E3E9032BEFFA991DFC7678F42C5CA2A4DDD5CDBD15A88" } 2017-11-03 10:48:30.0962 TRACE <<<< Exiting "SaveHistory" with return value: <void> 2017-11-03 10:48:30.1275 INFO Archiving working set. 2017-11-03 10:48:31.0963 DEBUG Working set deleted.