[C#] Show log to console Winform

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#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog

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:


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);
                }
            });
        }
    }
}

Chúc các bạn thành công với thủ thuật trên.

DOWNLOAD FULL SOURCE


PASSWORD UNZIP: HUNG.PRO.VN

PASSWORD UNZIP: HUNG.PRO.VN
Chúc Mọi Người Thành Công Với Thủ Thuật Trên.
Nếu mọi người có vướng mắc gì mình chia sẽ trên trang có thể gửi liên hê cho mình tại đây nhé.
Cảm ơn mọi người đã quan tâm.

2 comments:

Hạ Tỷ Tỷ said...

k hiểu gì a :D

Hung Program VN said...

đây là hiện thông tin mình hoạt động trên window e nhé.

All Right Reserved © 2015 By Hung Pro VN