using System; using ZeroLevel.Services.Logging; using ZeroLevel.Services.Logging.Implementation; namespace ZeroLevel { public static class Log { #region Fields internal static ILogger _router; #endregion Fields #region Ctor static Log() { _router = new LogRouter(); } #endregion Ctor private static string FormatMessage(string line, params object[] args) { if (args == null || args.Length == 0) return line; try { return string.Format(line, args); } catch { return line; } } #region Logging public static void Raw(string line, params object[] args) { _router.Write(LogLevel.Raw, FormatMessage(line, args)); } /// /// Info message /// public static void Info(string line, params object[] args) { _router.Write(LogLevel.Info, FormatMessage(line, args)); } /// /// Warning message /// public static void Warning(string line, params object[] args) { _router.Write(LogLevel.Warning, FormatMessage(line, args)); } /// /// Error message /// public static void Error(string line, params object[] args) { _router.Write(LogLevel.Error, FormatMessage(line, args)); } /// /// Error message /// public static void Error(Exception ex, string line, params object[] args) { _router.Write(LogLevel.Error, FormatMessage(line, args) + "\r\n" + ex.ToString()); } /// /// Fatal crash /// public static void Fatal(string line, params object[] args) { _router.Write(LogLevel.Fatal, FormatMessage(line, args)); } /// /// Fatal message (mean stop app after crash) /// public static void Fatal(Exception ex, string line, params object[] args) { _router.Write(LogLevel.Fatal, FormatMessage(line, args) + "\r\n" + ex.ToString()); } /// /// Debug message /// public static void Debug(string line, params object[] args) { _router.Write(LogLevel.Debug, FormatMessage(line, args)); } /// /// Low-level debug message /// public static void Verbose(string line, params object[] args) { _router.Write(LogLevel.Verbose, FormatMessage(line, args)); } /// /// System message /// public static void SystemInfo(string line, params object[] args) { _router.Write(LogLevel.SystemInfo, FormatMessage(line, args)); } /// /// System warning /// public static void SystemWarning(string line, params object[] args) { _router.Write(LogLevel.SystemWarning, FormatMessage(line, args)); } /// /// System error /// public static void SystemError(string line, params object[] args) { _router.Write(LogLevel.SystemError, FormatMessage(line, args)); } /// /// System error /// public static void SystemError(Exception ex, string line, params object[] args) { _router.Write(LogLevel.SystemError, FormatMessage(line, args) + "\r\n" + ex.ToString()); } #endregion Logging #region Register loggers public static void AddLogger(ILogger logger, LogLevel level = LogLevel.Standart) { (_router as ILogComposer)?.AddLogger(logger, level); } #region Delegate logger public static void AddDelegateLogger(Action handler, LogLevel level = LogLevel.Standart) { AddLogger(new DelegateLogger(handler), level); } #endregion Delegate logger #region Console public static void AddConsoleLogger(LogLevel level = LogLevel.Standart) { AddLogger(new ConsoleLogger(), level); } #endregion Console #region TextLogs public static void AddTextFileLogger(TextFileLoggerOptions options, LogLevel level = LogLevel.FullDebug) { AddLogger(new TextFileLogger(options), level); } public static void AddTextFileLogger(string path, LogLevel level = LogLevel.FullDebug) { AddLogger(new FileLogger(path), level); } #endregion TextLogs #region Encrypted file log public static void AddEncryptedFileLogger(EncryptedFileLogOptions options, LogLevel level = LogLevel.FullDebug) { AddLogger(new EncryptedFileLog(options), level); } #endregion Encrypted file log #endregion Register loggers #region Settings public static void CreateLoggingFromConfiguration() { CreateLoggingFromConfiguration(Configuration.ReadFromApplicationConfig()); } public static void CreateLoggingFromConfiguration(IConfiguration config) { if (config.FirstOrDefault("console", false)) { AddConsoleLogger(); } if (config.Contains("log")) { var options = TextFileLoggerOptions.CreateOptionsBy(config, "log"); if (options != null) { AddTextFileLogger(options); } } var debug = string.Empty; if (config.Contains("debug")) { debug = "debug"; } if (config.Contains("trace")) { debug = "trace"; } if (false == string.IsNullOrWhiteSpace(debug)) { var options = TextFileLoggerOptions.CreateOptionsBy(config, debug); if (options != null) { AddTextFileLogger(options, LogLevel.Debug); } } } /// /// Set max count log-messages in queue /// public static void Backlog(long backlog) { (_router as ILogComposer)?.SetupBacklog(backlog); } #endregion Settings #region Disposable public static void Dispose() { _router.Dispose(); } #endregion Disposable } }