/// <summary> /// Logs an exception. /// </summary> /// <param name="logLocation">The log location.</param> /// <param name="ex">The exception to log.</param> protected void LogException(LogLocation logLocation, Exception ex) { // Print friendly error parsable by Visual Studio in order to display them in the Error List var reader = new StringReader(ex.ToString()); // And write the exception parsable by Visual Studio string line; while ((line = reader.ReadLine()) != null) { Match match = regex.Match(line); if (match.Success) { string methodLocation = match.Groups[1].Value; string fileName = match.Groups[2].Value; int lineNumber; int.TryParse(match.Groups[3].Value, out lineNumber); Log(LogLevel.Error, new LogLocation(fileName, lineNumber, 1), methodLocation, "Exception", null); } else { // Escape a line Log(LogLevel.Error, logLocation, null, line.Replace("{", "{{").Replace("}", "}}"), null); } } }
/// <summary> /// Formats the message. /// </summary> /// <param name="logLevel">The log level.</param> /// <param name="logLocation">The log location.</param> /// <param name="context">The context.</param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> /// <param name="parameters">The parameters.</param> /// <returns></returns> public static string FormatMessage(LogLevel logLevel, LogLocation logLocation, string context, string message, Exception exception, params object[] parameters) { var lineMessage = new StringBuilder(); if (logLocation != null) lineMessage.AppendFormat("{0}({1},{2}): ", logLocation.File, logLocation.Line, logLocation.Column); // Write log parsable by Visual Studio var levelName = Enum.GetName(typeof (LogLevel), logLevel).ToLower(); lineMessage.AppendFormat("{0}:{1}", levelName == "fatal" ? "error:fatal":levelName , FormatMessage(context, message, parameters)); return lineMessage.ToString(); }
/// <summary> /// Logs the specified log message. /// </summary> /// <param name="logLevel">The log level</param> /// <param name="logLocation">The log location.</param> /// <param name="context">The context.</param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> /// <param name="parameters">The parameters.</param> public override void Log(LogLevel logLevel, LogLocation logLocation, string context, string message, Exception exception, params object[] parameters) { lock (this) { if (Output == null) return; string lineMessage = FormatMessage(logLevel, logLocation, context, message, exception, parameters); Output.WriteLine(lineMessage); Output.Flush(); if (exception != null) LogException(logLocation, exception); } }
/// <summary> /// Formats the message. /// </summary> /// <param name="logLevel">The log level.</param> /// <param name="logLocation">The log location.</param> /// <param name="context">The context.</param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> /// <param name="parameters">The parameters.</param> /// <returns></returns> public static string FormatMessage(LogLevel logLevel, LogLocation logLocation, string context, string message, Exception exception, params object[] parameters) { var lineMessage = new StringBuilder(); if (logLocation != null) { lineMessage.AppendFormat("{0}({1},{2}): ", logLocation.File, logLocation.Line, logLocation.Column); } // Write log parsable by Visual Studio var levelName = Enum.GetName(typeof(LogLevel), logLevel).ToLower(); lineMessage.AppendFormat("{0}:{1}", levelName == "fatal" ? "error:fatal":levelName, FormatMessage(context, message, parameters)); return(lineMessage.ToString()); }
/// <summary> /// Logs the specified log message. /// </summary> /// <param name="logLevel">The log level</param> /// <param name="logLocation">The log location.</param> /// <param name="context">The context.</param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> /// <param name="parameters">The parameters.</param> public override void Log(LogLevel logLevel, LogLocation logLocation, string context, string message, Exception exception, params object[] parameters) { lock (this) { if (Output == null) { return; } string lineMessage = FormatMessage(logLevel, logLocation, context, message, exception, parameters); Output.WriteLine(lineMessage); Output.Flush(); if (exception != null) { LogException(logLocation, exception); } } }
/// <summary> /// Logs an exception. /// </summary> /// <param name="logLocation">The log location.</param> /// <param name="ex">The exception to log.</param> protected void LogException(LogLocation logLocation, Exception ex) { // Print friendly error parsable by Visual Studio in order to display them in the Error List var reader = new StringReader(ex.ToString()); // And write the exception parsable by Visual Studio string line; while ((line = reader.ReadLine()) != null) { Match match = regex.Match(line); if (match.Success) { string methodLocation = match.Groups[1].Value; string fileName = match.Groups[2].Value; int lineNumber; int.TryParse(match.Groups[3].Value, out lineNumber); Log( LogLevel.Error, new LogLocation(fileName, lineNumber, 1), methodLocation, "Exception", null); } else { // Escape a line Log(LogLevel.Error, logLocation, null, line.Replace("{", "{{").Replace("}", "}}"), null); } } }
/// <summary> /// Logs the specified log message. /// </summary> /// <param name="logLevel">The log level</param> /// <param name="logLocation">The log location.</param> /// <param name="context">The context.</param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> /// <param name="parameters">The parameters.</param> public abstract void Log(LogLevel logLevel, LogLocation logLocation, string context, string message, Exception exception, params object[] parameters);