/// <summary> /// Marks the job as cancelled, changing the event written when the job completes. /// </summary> public void Cancel() { #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); #else _Cancelled = true; #endif }
public RecyclableStringWriter(StringBuilder stringBuilder, IFormatProvider formatProvider) : base(stringBuilder, formatProvider) { if (stringBuilder == null) { throw new ArgumentNullException(nameof(stringBuilder)); } #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); #else _MaxCapacity = stringBuilder.MaxCapacity; _DefaultCapacity = stringBuilder.Capacity; #endif }
/// <summary> /// Clears or resets all internal state of this instance so it can be reused. /// </summary> public void Reset() { #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); #else _Logger = null; _JobLogger = null; _JobName = null; _JobId = null; _Exception = null; _Properties = null; _Stopwatch.Reset(); #endif }
/// <summary> /// Returns true if the exception is of a type that indicates a serious system flaw, one that has likely resulted in unknown process state and should not attempt to be handled at all. /// </summary> /// <param name="exception">The exception to test.</param> /// <returns>True if the exception should be rethrown immediately, else false.</returns> public static bool ShouldRethrowImmediately(this Exception exception) { #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); return(false); #else return (#if !NETFX_CORE exception is StackOverflowException || exception is System.Threading.ThreadAbortException || exception is AccessViolationException || #endif exception is OutOfMemoryException); #endif }
public void Initialize(ILogger logger, string jobName, string jobId, IEnumerable <KeyValuePair <string, object> > properties, ILoggedJobPool parentPool, TimeSpan maxExpectedDuration) { if (logger == null) { throw new ArgumentNullException(nameof(logger)); } if (jobName == null) { throw new ArgumentNullException(nameof(jobName)); } if (String.IsNullOrWhiteSpace(jobName)) { throw new ArgumentException(String.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.PropertyCannotBeEmptyOrWhitespace, jobName), nameof(jobName)); } if (String.IsNullOrWhiteSpace(jobId)) { jobId = Guid.NewGuid().ToString(); } #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); #else _ParentPool = parentPool; _Stopwatch = new System.Diagnostics.Stopwatch(); _Logger = logger; _JobId = jobId ?? Guid.NewGuid().ToString(); _JobName = jobName; _MaxExpectedDuration = maxExpectedDuration; _Properties = GetExtendedProperties(properties, new KeyValuePair <string, object>(Properties.Resources.JobNamePropertyName, jobName), new KeyValuePair <string, object>(Properties.Resources.JobIdPropertyName, jobId) ).ToArray(); _Logger.WriteEvent(String.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.JobStartedEventMessage, jobName, jobId), eventSeverity: LogEventSeverity.Information, eventType: LogEventType.Start, properties: _Properties ); _Stopwatch.Start(); #endif }
private static List <System.Reflection.PropertyInfo> GetProperties(Type type) { #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); return(null); #else List <System.Reflection.PropertyInfo> retVal = null; if (!s_PropertyCache.TryGetValue(type, out retVal)) { #if !NETFX_CORE retVal = new List <System.Reflection.PropertyInfo>(type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)); #else retVal = new List <System.Reflection.PropertyInfo>(type.GetTypeInfo().DeclaredProperties); #endif s_PropertyCache.TryAdd(type, retVal); } return(retVal); #endif }
/// <summary> /// Creates a new job from the same pool, using the same logger and original properties as well as the new properties provided, and with a "Parent Job ID" property containing the Id of this job for correlation purposes. /// </summary> /// <param name="jobName">The name or type of the job to create.</param> /// <param name="jobId">A unique id of the job used to correlate log entries.</param> /// <param name="properties">Null, or a collection key value pairs to associate with the child job.</param> /// <returns>A <see cref="LoggedJob"/> instance used to track the job and log related entries.</returns> public LoggedJob CreateChildJob(string jobName, string jobId, params KeyValuePair <string, object>[] properties) { #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); return(null); #else var retVal = _ParentPool?.Take() ?? new LoggedJob(); IEnumerable <KeyValuePair <string, object> > allProperties = new KeyValuePair <string, object>[] { new KeyValuePair <string, object>(Properties.Resources.ParentJobIdPropertyName, this._JobId) }; if (properties != null) { allProperties = allProperties.Union(GetNonJobProperties(properties)); } if (_Properties != null) { allProperties = allProperties.Union(GetNonJobProperties(_Properties)); } retVal.Initialize(_Logger, jobName, jobId, allProperties, _ParentPool, TimeSpan.Zero); return(retVal); #endif }
/// <summary> /// Records a failure associated with the job. Will also cause the job completion message to be written with an error severity instead of information. /// </summary> /// <param name="exception">The exception representing the failure.</param> /// <remarks> /// <para>If multiple exceptions are recorded, each will be logged individually but the completion message will reference only the first exception.</para> /// </remarks> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="exception"/> is null.</exception> public void SetFailure(Exception exception) { if (exception == null) { throw new ArgumentNullException(nameof(exception)); } #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); #else if (_Exception == null) { _Exception = exception; } _Logger.WriteEventWithSource(String.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.JobFailureEventMessage, _JobName, _JobId, exception.GetType().FullName + ":" + exception.Message), eventSeverity: LogEventSeverity.Error, eventType: LogEventType.Failure, properties: GetExtendedProperties(_Properties, new KeyValuePair <string, object>("Exception", exception)).ToArray() ); #endif }
/// <summary> /// Stops timing and writes the job completed event. If a parent pool was supplied, returns the item to the pool. /// </summary> public void Dispose() { #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); #else //If we've been 'reset' and returned to a pool, we don't need to log anything when we're disposed. if (_Logger != null) { var logger = _Logger; _Logger = null; _Stopwatch.Stop(); var severity = LogEventSeverity.Information; if (_Exception != null) { severity = LogEventSeverity.Error; } else if (_Cancelled || (_Stopwatch.Elapsed > _MaxExpectedDuration && _MaxExpectedDuration != TimeSpan.Zero)) { severity = LogEventSeverity.Warning; } logger?.WriteEvent(String.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.JobFinishedEventMessage, _JobName, _JobId), eventSeverity: severity, eventType: _Cancelled ? LogEventType.Canceled : LogEventType.Completed, properties: GetExtendedProperties( _Properties, new KeyValuePair <string, object>("Exception", _Exception), new KeyValuePair <string, object>("Duration", _Stopwatch.Elapsed), new KeyValuePair <string, object>("Outcome", _Exception != null ? Properties.Resources.FailedOutcome : (_Cancelled ? Properties.Resources.CancelledOutcome : Properties.Resources.CompletedOutcome)) ).ToArray() ); _ParentPool?.Add(this); } #endif }
/// <summary> /// Default constructor. /// </summary> public LoggedJob() { #if CONTRACTS_ONLY BaitExceptionHelper.Throw(); #endif }