'Is it possible to display Serilog log in the program's GUI?

With the logging system Serilog is it possible to display the log in a text box, or a list view or some other GUI control; what is the mechanism to get it there?



Solution 1:[1]

You might want to check out Serilog.Sinks.RichTextBox sink which can write logs to a RichTextBox control with colors, formatting, etc. similar to what you'd expect in a Console application (you have full control of the colors through themes).

Serilog.Sinks.RichTextBox

The sink was originally built for WPF applications, but can easily be used with Windows Forms application as well and there's an example in the repository that shows how to use it with Windows Forms.

Disclaimer: I'm the author of the Serilog.Sinks.RichTextBox sink.

Solution 2:[2]

You can use Serilog.Sinks.TextWriter

You can display the logs on the multi-line textbox like below;

namespace SerilogSample
{
    public partial class Form1 : Form
    {
        private StringWriter _messages;
        ILogger _logger;
        public Form1()
        {
            InitializeComponent();

            _messages = new StringWriter();
            _logger = new LoggerConfiguration()
                .WriteTo.TextWriter(_messages)
                .CreateLogger();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _logger.Information("Clicked!");
            textBox1.Text = _messages.ToString();
        }
    }
}

The result;

enter image description here

Solution 3:[3]

I haven't tried it, but this package looks promising for Windows Forms:

I also started with the accepted answer and came up with my own bare-bones Windows Forms TextBox sink:

Custom sink

    public class TextBoxSink : ILogEventSink
{
    private readonly TextBox textBox;
    private readonly LogEventLevel minLevel;
    private readonly ITextFormatter formatter;

    public TextBoxSink(
        TextBox textBox,
        LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose,
        string outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u5}: {Message:lj}{NewLine}")
    {
        this.textBox = textBox;
        minLevel = restrictedToMinimumLevel;
        formatter = new MessageTemplateTextFormatter(outputTemplate);
    }

    public void Emit(LogEvent logEvent)
    {
        if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
        if (logEvent.Level < minLevel) return;

        var sw = new StringWriter();
        formatter.Format(logEvent, sw);

        textBox.InvokeIfRequired(() => textBox.AppendText(sw.ToString()));
    }
}

Extension method (used above to manipulate the text box from any thread; source)

public static class WinFormsControlExtensions
{
    public static void InvokeIfRequired(this Control c, MethodInvoker action)
    {
        if (c.InvokeRequired)
        {
            c.Invoke(action);
        }
        else
        {
            action();
        }
    }
}

Usage

public partial class Form1 : Form
{
    public System.Windows.Forms.TextBox LogTextBox;

    private void Form1_Shown(object sender, EventArgs e)
    {
        Logger.Log = new LoggerConfiguration()
            .WriteTo.Sink(new TextBoxSink(LogTextBox))
            .CreateLogger();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 C. Augusto Proiete
Solution 2 Tolga Cakir
Solution 3