Exemple #1
0
        /// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <param name="entry">The log entry</param>
        /// <returns>Is written or not</returns>
        public bool Write(LogEntry entry)
        {
            var context = LoggerContext.Current;

            return context.Write(entry);
        }
Exemple #2
0
        /// <summary>
        /// Writes a log entry asyncronously.
        /// </summary>
        /// <param name="entry">The log entry</param>
        /// <returns>Is written or not</returns>
        public Task<bool> WriteAsync(LogEntry entry)
        {
            var context = LoggerContext.Current;

            return context.WriteAsync(entry);
        }
        /// <summary>
        /// Applies a custom format to a log entry.
        /// </summary>
        /// <param name="input">The format string</param>
        /// <param name="entry">The log entry</param>
        /// <param name="forceNoNewLine">Force no new line</param>
        /// <returns>Formatted message</returns>
        public string ApplyCustom(string input, LogEntry entry, bool forceNoNewLine = false)
        {
            string formatted;

            if (entry.Flags.HasFlag(LogFlags.Direct))
            {
                formatted = entry.Message;
            }
            else
            {
                formatted = Regex.Replace(
                    input,
                    @"{([^:}]*)(:[^}]*)?}",
                    (match) =>
                    {
                        string format;
                        if (match.Groups.Count >= 2)
                        {
                            format = match.Groups[2].Value.TrimStart(':');
                        }
                        else
                        {
                            format = null;
                        }

                        string text;
                        var formatUsed = false;

                        switch (match.Groups[1].Value)
                        {
                            case "application":
                                text = entry.Application ?? string.Empty;
                                break;
                            case "thread":
                                text = entry.ThreadFrom ?? string.Empty;
                                break;
                            case "category":
                                text = entry.Category ?? string.Empty;
                                break;
                            case "severity":
                            case "level":
                                text = entry.Severity.ToString("G").ToUpperInvariant();
                                break;
                            case "date":
                            case "timestamp":
                                formatUsed = true;

                                var dateFormat = format ?? "dd/MM/yyyy HH:mm:ss";
                                text = entry.Date.ToString(dateFormat, CultureInfo.InvariantCulture);
                                break;
                            case "message":
                                text = entry.Message;
                                break;
                            case "exception":
                                if (entry.Exception != null)
                                {
                                    var exceptionText = new StringBuilder();
                                    exceptionText.Append("Exception: ");
                                    exceptionText.AppendLine(entry.Exception.GetType().Name);

                                    exceptionText.Append("Message: ");
                                    exceptionText.AppendLine(entry.Exception.Message);

                                    exceptionText.Append("Source: ");
                                    exceptionText.AppendLine(entry.Exception.Source);

                                    exceptionText.AppendLine("=== Stack Trace ===");
                                    exceptionText.AppendLine(entry.Exception.StackTrace);

                                    text = exceptionText.ToString();
                                    break;
                                }

                                text = string.Empty;
                                break;
                            default:
                                text = match.Groups[0].Value;
                                break;
                        }

                        if (!formatUsed && !string.IsNullOrEmpty(format))
                        {
                            return text.PadRight(int.Parse(format));
                        }

                        return text;
                    });
            }

            var output = new StringBuilder();
            var newNewLine = Environment.NewLine + "-- ";
            output.Append(formatted.Replace(Environment.NewLine, newNewLine));

            if (!forceNoNewLine && !entry.Flags.HasFlag(LogFlags.NoNewLine))
            {
                output.Append(Environment.NewLine);
            }

            return output.ToString();
        }
 /// <summary>
 /// Applies a format to a log entry.
 /// </summary>
 /// <param name="format">The format</param>
 /// <param name="entry">The log entry</param>
 /// <returns>Formatted message</returns>
 public string Apply(string format, LogEntry entry)
 {
     return this.ApplyCustom(this.formats[format], entry);
 }
 // methods
 /// <summary>
 /// Applies the default format to a log entry.
 /// </summary>
 /// <param name="entry">The log entry</param>
 /// <returns>Formatted message</returns>
 public string Apply(LogEntry entry)
 {
     return this.Apply(this.defaultFormat, entry);
 }
 /// <summary>
 /// Writes the specified log entry.
 /// </summary>
 /// <param name="entry">The log entry</param>
 /// <returns>Is written or not</returns>
 public Task<bool> WriteAsync(LogEntry entry)
 {
     return Task.Factory.StartNew<bool>(
         () => this.Write(entry));
 }
        // methods
        /// <summary>
        /// Writes the specified log entry.
        /// </summary>
        /// <param name="entry">The log entry</param>
        /// <returns>Is written or not</returns>
        public bool Write(LogEntry entry)
        {
            if (!this.disabled && this.LogEntryPopped != null)
            {
                if (entry.Severity >= this.minimumSeverity)
                {
                    this.LogEntryPopped(this, entry);

                    return true;
                }
            }

            return false;
        }