BomKhung.Com Đã Quay Trở Lại
Hoạt Động Dưới Tên Miền Hung.Pro.VN
00 Days
00 Hours
00 Minutes
00 Seconds
Hiện website đang cập nhật nội dung bài viết, nếu có lỗi gì mọi người có thể thông báo cho mình Tại đây!

[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 SOURCE CODE

PASSWORD UNZIP: HUNG.PRO.VN

Đăng nhận xét

Đồng ý sữ dụng cookie
Chúng tôi sử dụng cookie trên trang web này để phân tích lưu lượng truy cập, ghi nhớ tùy chọn của bạn và tối ưu hóa trải nghiệm của bạn.
Xem thêm
Oops!
Có vẻ như kết nối internet của bạn có vấn đề. Vui lòng kết nối lại và duyệt web.
AdBlock Detected!
Chúng tôi phát hiện bạn đang sử dụng plugin chặn quảng cáo trong trình duyệt của mình.
Doanh thu chúng tôi kiếm được từ quảng cáo được sử dụng để quản lý trang web này, chúng tôi yêu cầu bạn đưa trang web của chúng tôi vào danh sách trắng trong plugin chặn quảng cáo của bạn.
Site is Blocked
Sorry! This site is not available in your country.
Kỹ thuật số thế hệ tiếp theo Chào mừng bạn đến với trò chuyện WhatsApp
Xin chào! Chúng tôi có thể giúp gì cho bạn hôm nay?
Nhập vào đây...