NEWS

[C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog

[C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
Đăng bởi: Thảo Meo - Lượt xem: 1406 13:57:30, 04/01/2024C#   In bài viết

Xin chào các  bạn, bài viết hôm nay mình hướng dẫn các bạn sử dụng Serialog để show log ra Richtextbox giống trên console windows.

[C#] Show log to console Winform

Dưới đây, là hình ảnh demo ứng dụng:

Đầu tiên, các bạn cài đặt cho mình thư viện Serilog bên dưới từ Nuget.

NuGet\Install-Package Serilog.Sinks.RichTextBox.WinForms.Colored -Version 1.2.0

Giao diện show log ứng dụng:

serial_log_winform

Các bạn có thể show nhiều định dạng log như: Error, Warning, Information, Faltal...

Xem video hướng dẫn thực hiện step by step.

Full source code C# Form1.cs:

using Serilog.Events;
using Serilog.Sinks.RichTextBoxForms.Themes;
using Serilog.Sinks.RichTextBoxForms;
using Serilog;
using System.Windows.Forms;

namespace SerilogDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private RichTextBoxSinkOptions _options = null!;

        private void Initialize()
        {
            _options = new RichTextBoxSinkOptions(ThemePresets.Dark, 200, 5, true);
            var sink = new RichTextBoxSink(richTextBox1, _options);
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Verbose()
                .WriteTo.Sink(sink, LogEventLevel.Verbose)
                .CreateLogger();

            Log.Debug("Started logger.");

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Log.Error("Error messge Oops....");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Log.Information("Error messge Oops....");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Log.Warning("Error messge Oops....");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var frm = new Form2();
            frm.Show();
        }
    }
}

Source code Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Serilog;

namespace SerilogDemo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    Log.Information($"Processing line: {i}");
                    Thread.Sleep(300);

                    Log.Fatal($"Falal line: {i}");
                    Thread.Sleep(300);
                }
            });
        }
    }
}

Thanks for watching!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
Đăng bởi: Thảo Meo - Lượt xem: 1406 13:57:30, 04/01/2024C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.