/// <summary> /// This logging is done with simple methods like local logging. The <paramref name="message"/> will be extended with application information and possibly exception information. /// </summary> /// <param name="severityLevel">The severity level for this log.</param> /// <param name="message">The message to log.</param> /// <param name="exception">An optional exception that will have it's information incorporated in the message.</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> public static void FallbackSafeLog( LogSeverityLevel severityLevel, string message, Exception exception = null, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0) { try { var hideStackTrace = severityLevel < LogSeverityLevel.Error; // ReSharper disable ExplicitCallerInfoArgument var location = LocationToLogString(memberName, filePath, lineNumber); // ReSharper restore ExplicitCallerInfoArgument var messageWithLogInfo = $"{severityLevel}: {message}"; messageWithLogInfo += $"\r{FulcrumApplication.ToLogString()}"; messageWithLogInfo += ContextToLogString(); messageWithLogInfo += $"\r{location}"; if (exception != null) { messageWithLogInfo += $"\r{exception.ToLogString(hideStackTrace)}"; } FulcrumApplication.Setup.FallbackLogger.SafeLog(severityLevel, messageWithLogInfo); } catch (Exception) { // We give up } }
public void FallbackSafeLog() { const LogSeverityLevel expectedLevel = LogSeverityLevel.Error; const string exceptionMessage = "ExceptionMessage"; const string message = "TestMessage"; try { throw new TestException(exceptionMessage); } catch (Exception expectedException) { // ReSharper disable ExplicitCallerInfoArgument LogHelper.FallbackSafeLog(expectedLevel, message, expectedException, "memberName", "filePath", 42); // ReSharper restore ExplicitCallerInfoArgument UT.Assert.AreEqual(expectedLevel, _loggedSeverityLevel); UT.Assert.IsNotNull(_loggedMessage); // ReSharper disable ExplicitCallerInfoArgument UT.Assert.IsTrue(_loggedMessage.Contains(LogHelper.LocationToLogString("memberName", "filePath", 42))); // ReSharper restore ExplicitCallerInfoArgument UT.Assert.IsTrue(_loggedMessage.Contains(FulcrumApplication.ToLogString())); } }