Exemple #1
0
        /// <summary>
        /// Use this method to log when the original logging method fails.
        /// </summary>
        /// <param name="message">What went wrong with logging</param>
        /// <param name="logRecord">The message to log.</param>
        /// <param name="exception">If what went wrong had an exception</param>
        /// <param name="memberName">Method or property name of the caller</param>
        /// <param name="filePath">Full path of the source file that contains the caller. This is the file path at compile time.</param>
        /// <param name="lineNumber">Line number in the source file at which the method is called</param>
        internal static void FallbackToSimpleLoggingFailSafe(string message, LogRecord logRecord,
                                                             Exception exception = null,
                                                             [CallerMemberName] string memberName = "",
                                                             [CallerFilePath] string filePath     = "",
                                                             [CallerLineNumber] int lineNumber    = 0)
        {
            if (logRecord == null)
            {
                return;
            }
            try
            {
                var totalMessage = message == null ? "" : $"{message}\r";
                if (exception == null)
                {
                    totalMessage += "The logging mechanism itself failed and is using a fallback method.";
                }

                // If a message of warning or higher ends up here means it is critical, since this log will not end up in the normal log.
                var severityLevel = logRecord.IsGreaterThanOrEqualTo(LogSeverityLevel.Warning)
                    ? LogSeverityLevel.Critical
                    : LogSeverityLevel.Warning;
                string logRecordAsString;
                try
                {
                    logRecordAsString = JsonConvert.SerializeObject(logRecord);
                }
                catch (Exception)
                {
                    logRecordAsString = logRecord.ToLogString();
                }
                totalMessage += $"\r{logRecordAsString}";
                // ReSharper disable ExplicitCallerInfoArgument
                FallbackSafeLog(severityLevel, totalMessage, exception, memberName, filePath, lineNumber);
                // ReSharper restore ExplicitCallerInfoArgument
            }
            catch (Exception e)
            {
                FallbackSafeLog(LogSeverityLevel.Critical, $"Failed to log a message.", e);
            }
        }
Exemple #2
0
 /// <inheritdoc />
 public void LogSync(LogRecord logRecord)
 {
     SafeLog(logRecord.SeverityLevel, logRecord.ToLogString());
 }