/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"> /// <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only /// unmanaged resources. /// </param> protected virtual void Dispose(bool disposing) { if (!_disposed) { ///// // If called by the finalizer, dont bother restoring the context as you // are currently running on a GC Finalizer thread. ///// if (disposing) { ///// // Restore the original context ///// if (_originalContextInfo != null) { _originalContextInfo.TransactionId = TransactionId; CallContext.LogicalSetData(CallContextKey, _originalContextInfo); } else { CallContext.FreeNamedDataSlot(CallContextKey); } } _originalContextInfo = null; ///// // Dispose complete. ///// _disposed = true; } }
/// <summary> /// Gets the message chain. /// </summary> /// <param name="userId">The user identifier.</param> /// <param name="separator">The separator.</param> /// <param name="maxLength">The maximum length.</param> /// <param name="caller">The caller.</param> /// <returns></returns> /// <value> /// The message chain. /// </value> public static string GetMessageChain(long userId, string separator = "->", int maxLength = 128, [CallerMemberName] string caller = null) { DatabaseContextInfo ctx = GetContextInfo( ); if (ctx != null && ctx.TransactionId >= 0) { return($"u:{userId},{ctx.TransactionId}"); } IList <string> messageChain = ctx == null?BuildDebugMessageChain( ) : BuildContextInfoMessageChain(ctx); string message = TruncateMessageChain(userId, messageChain, separator, maxLength); message = message ?? caller ?? "No context available"; return($"u:{userId},{message}"); }
/// <summary> /// Builds the context information message chain. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> private static IList <string> BuildContextInfoMessageChain(DatabaseContextInfo context) { List <string> messageChain = new List <string>( ); DatabaseContextInfo currentContext = context; while (currentContext != null) { if (!string.IsNullOrEmpty(currentContext.Message)) { messageChain.Insert(0, currentContext.Message); } currentContext = currentContext.Parent; } return(messageChain); }
/// <summary> /// Initializes a new instance of the <see cref="DatabaseContextInfo" /> class. /// </summary> /// <param name="message">The message.</param> public DatabaseContextInfo(string message) { DatabaseContextInfo existingContextInfo = CallContext.LogicalGetData(CallContextKey) as DatabaseContextInfo; if (existingContextInfo != null) { _originalContextInfo = existingContextInfo; TransactionId = _originalContextInfo.TransactionId; } else { TransactionId = -1; } ///// // Compact messages ///// if (existingContextInfo == null || !string.Equals(existingContextInfo.Message, message, StringComparison.OrdinalIgnoreCase)) { Message = message; } CallContext.LogicalSetData(CallContextKey, this); }
/// <summary> /// Gets the context information. /// </summary> /// <returns></returns> public static DatabaseContextInfo GetContextInfo( ) { DatabaseContextInfo contextInfo = CallContext.LogicalGetData(CallContextKey) as DatabaseContextInfo; return(contextInfo); }