#region S# License /****************************************************************************************** NOTICE!!! This program and source code is owned and licensed by StockSharp, LLC, www.stocksharp.com Viewing or use of this code requires your acceptance of the license agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE Removal of this comment is a violation of the license agreement. Project: StockSharp.Logging.Logging File: LoggingHelper.cs Created: 2015, 11, 11, 2:32 PM Copyright 2010 by StockSharp, LLC *******************************************************************************************/ #endregion S# License namespace StockSharp.Logging { using System; using System.Linq; using System.Reflection; using Ecng.Common; using Ecng.Configuration; /// /// Extension class for . /// public static class LoggingHelper { /// /// To record a message to the log. /// /// Logs receiver. /// The function returns the text for . public static void AddInfoLog(this ILogReceiver receiver, Func getMessage) { receiver.AddLog(LogLevels.Info, getMessage); } /// /// To record a warning to the log. /// /// Logs receiver. /// The function returns the text for . public static void AddWarningLog(this ILogReceiver receiver, Func getMessage) { receiver.AddLog(LogLevels.Warning, getMessage); } /// /// To record an error to the log. /// /// Logs receiver. /// The function returns the text for . public static void AddErrorLog(this ILogReceiver receiver, Func getMessage) { receiver.AddLog(LogLevels.Error, getMessage); } /// /// To record a message to the log. /// /// Logs receiver. /// The level of the log message. /// The function returns the text for . public static void AddLog(this ILogReceiver receiver, LogLevels level, Func getMessage) { if (receiver == null) throw new ArgumentNullException(nameof(receiver)); receiver.AddLog(new LogMessage(receiver, receiver.CurrentTime, level, getMessage)); } /// /// To record a message to the log. /// /// Logs receiver. /// Text message. /// Text message settings. Used if a message is the format string. For details, see . public static void AddInfoLog(this ILogReceiver receiver, string message, params object[] args) { receiver.AddMessage(LogLevels.Info, message, args); } /// /// To record a verbose message to the log. /// /// Logs receiver. /// Text message. /// Text message settings. Used if a message is the format string. For details, see . public static void AddVerboseLog(this ILogReceiver receiver, string message, params object[] args) { receiver.AddMessage(LogLevels.Verbose, message, args); } /// /// To record a debug message to the log. /// /// Logs receiver. /// Text message. /// Text message settings. Used if a message is the format string. For details, see . public static void AddDebugLog(this ILogReceiver receiver, string message, params object[] args) { receiver.AddMessage(LogLevels.Debug, message, args); } /// /// To record a warning to the log. /// /// Logs receiver. /// Text message. /// Text message settings. Used if a message is the format string. For details, see . public static void AddWarningLog(this ILogReceiver receiver, string message, params object[] args) { receiver.AddMessage(LogLevels.Warning, message, args); } /// /// To record an error to the log. /// /// Logs receiver. /// Error details. public static void AddErrorLog(this ILogReceiver receiver, Exception exception) { receiver.AddErrorLog(exception, null); } /// /// To record an error to the log. /// /// Logs receiver. /// Error details. /// A format string. public static void AddErrorLog(this ILogReceiver receiver, Exception exception, string format) { if (receiver == null) throw new ArgumentNullException(nameof(receiver)); if (exception == null) throw new ArgumentNullException(nameof(exception)); receiver.AddLog(new LogMessage(receiver, receiver.CurrentTime, LogLevels.Error, () => { var msg = exception.ToString(); if (exception is ReflectionTypeLoadException refExc) { msg += Environment.NewLine + refExc .LoaderExceptions .Select(e => e.ToString()) .Join(Environment.NewLine); } if (format != null) msg = format.Put(msg); return msg; })); } /// /// To record an error to the log. /// /// Logs receiver. /// Text message. /// Text message settings. Used if a message is the format string. For details, see . public static void AddErrorLog(this ILogReceiver receiver, string message, params object[] args) { receiver.AddMessage(LogLevels.Error, message, args); } private static void AddMessage(this ILogReceiver receiver, LogLevels level, string message, params object[] args) { if (receiver == null) throw new ArgumentNullException(nameof(receiver)); if (level < receiver.LogLevel) return; receiver.AddLog(new LogMessage(receiver, receiver.CurrentTime, level, message, args)); } /// /// To record an error to the . /// /// Error. /// A format string. public static void LogError(this Exception error, string format = null) { if (error == null) throw new ArgumentNullException(nameof(error)); var manager = ConfigManager.TryGetService(); if (manager != null) manager.Application.AddErrorLog(error, format); } /// /// Get for the source. If the value is equal to , then parental source level is taken. /// /// The log source. /// Logging level. public static LogLevels GetLogLevel(this ILogSource source) { if (source == null) throw new ArgumentNullException(nameof(source)); do { var level = source.LogLevel; if (level != LogLevels.Inherit) return level; source = source.Parent; } while (source != null); return LogLevels.Inherit; } /// /// Wrap the specified action in try/catch clause with logging. /// /// The action. public static void DoWithLog(this Action action) { if (action == null) throw new ArgumentNullException(nameof(action)); try { action(); } catch (Exception ex) { ex.LogError(); } } /// /// Wrap the specified action in try/catch clause with logging. /// /// The type of returned result. /// The action. /// The resulting value. public static T DoWithLog(this Func action) { if (action == null) throw new ArgumentNullException(nameof(action)); try { return action(); } catch (Exception ex) { ex.LogError(); return default(T); } } } }