/// <summary> /// Log a block of code entry, exit, and timing /// </summary> /// <param name="blockName">The name of the code block being logged</param> /// <param name="options">The log options</param> /// <returns>A disposable object or none if logging is disabled</returns> public static IDisposable EntryExitLog(string blockName, EntryExitLoggerOptions options = EntryExitLoggerOptions.AlwaysLog) { IDisposable logger = null; if (Log.LoggingLevel == LogSeverity.Trace || ((options & EntryExitLoggerOptions.AlwaysLog) == EntryExitLoggerOptions.AlwaysLog)) { // Check if ExecutionTime logging is requested, and if so log if Verbose logging (or greater) is chosen bool shouldCreate = ((options & EntryExitLoggerOptions.ExecutionTime) == EntryExitLoggerOptions.ExecutionTime); // If not logging ExecutionTime log only if Entry or Exit tracing is requested if (!shouldCreate) { shouldCreate = (((options & EntryExitLoggerOptions.Entry) == EntryExitLoggerOptions.Entry) || ((options & EntryExitLoggerOptions.Exit) == EntryExitLoggerOptions.Exit)); } // Check if we actually need to log anything if (shouldCreate) { logger = new EntryExitLogger(blockName, options); } } // Will return null if no method logger was needed - which will effectively be ignored in a using statement. return(logger); }
/// <summary> /// Ctor private - just called from the static MethodLog method /// </summary> /// <param name="blockName">The name of the method being logged</param> /// <param name="options">The log options</param> private EntryExitLogger(string blockName, EntryExitLoggerOptions options) { this.blockName = blockName; this.options = options; this.alwaysLog = ((this.options & EntryExitLoggerOptions.AlwaysLog) == EntryExitLoggerOptions.AlwaysLog); if ((this.options & EntryExitLoggerOptions.ExecutionTime) == EntryExitLoggerOptions.ExecutionTime) { this.sw = new Stopwatch(); this.sw.Start(); } if ((this.options & EntryExitLoggerOptions.Entry) == EntryExitLoggerOptions.Entry) { Log.Trace("block entry", this.blockName, this.alwaysLog); } }