Serilog x RichTextBox ์†Œ๊ฐœ | Serilog.Sinks.RichTextBox.Wpf

image

Serilog.Sinks.RichTextBox.Wpf


์•ˆ๋…•ํ•˜์„ธ์š”! ๊ฐœ๋ฐœ์ž ์ด์žฌ์›…์ž…๋‹ˆ๋‹ค.
์—ฌ๋Ÿฌ๋ถ„์€ ๋กœ๊ทธ๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ๊ณ„์‹ ๊ฐ€์š”?

์•„๋งˆ ์ผ๋ฐ˜์ ์œผ๋กœ ์ง์ ‘ ๊ตฌํ˜„์„ ํ•˜๊ฑฐ๋‚˜ ์‹ ๋ขฐ ์žˆ๋Š” ๋กœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ณ„์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งŽ์€ ๋ถ„๋“ค์ด ์‚ฌ์šฉํ•˜๋Š” Serilog๋Š” ๋น„์ฃผ์–ผ ์ ์œผ๋กœ ์ข€ ๋” ์žฌ๋ฏธ์žˆ๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ™•์žฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


WPF์—์„œ RichTextBox๋ฅผ ์‚ฌ์šฉํ•œ ๋ชจ์Šตโ€ฆ

image

(๋งˆ์น˜ ์ฝ˜์†” ๊ฐ™์ฃ ?)

์‚ฌ์šฉ ๋ฐฉ๋ฒ•

์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ์•„์ฃผ ์‹ฌํ”Œํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € Serilog.Sinks.RichTextBox.Wpf ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Install-Package Serilog.Sinks.RichTextBox.Wpf -Prerelease

(๋˜๋Š” Nuget์„ ํ†ตํ•ด ์„ค์น˜)


1. RichTextBox ์ปจํŠธ๋กค ์ƒ์„ฑ

์•„๋ž˜์™€ ๊ฐ™์ด ํ‰๋ฒ”ํ•œ RichTextBox๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.

<RichTextBox x:Name="MyRichTextBox"
    Background="Black"
    Foreground="LightGray"
    FontFamily="Cascadia Mono, Consolas, Courier New, monospace"
    VerticalScrollBarVisibility="Auto"/>

2. RichTextBox ์ปจํŠธ๋กค ์—ฐ๊ฒฐ

Serilog์—์„œ ๋Œ€์ƒ RichTextBox์œผ๋กœ ๋กœ๊ทธ๋ฅผ ์ „๋‹ฌํ•ด์ค„ ์ˆ˜ ์žˆ๋„๋ก ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

Log.Logger = new LoggerConfiguration()
    .WriteTo.RichTextBox(MyRichTextBox)
    .CreateLogger();

3. Serilog ์‚ฌ์šฉ

RichTextBox๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ๋‚˜์„œ ์ž์œ ๋กญ๊ฒŒ ๋กœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

public partial class JamesWindow : Window
{
    public JamesWindow()
    {
        InitializeComponent();

        Log.Logger = new LoggerConfiguration()
            .WriteTo.RichTextBox(MyRichTextBox)
            .CreateLogger();

        Log.Information("InitializeComponent {0}", this.GetHashCode());

        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Log.Information("MainWindow_Loaded {0}", this);
    }
}

๋์œผ๋กœโ€ฆ

๊ทธ๋ฆฌ๊ณ  ์ด ํ™•์žฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์—ญ์‹œ GitHub ์˜คํ”ˆ์†Œ์Šค๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


๋‚ด๋ถ€์ ์ธ RichTextBox ์ฒ˜๋ฆฌ๊ฐ€ ๊ถ๊ตผํ•˜์‹  ๋ถ„์€ ์•„๋ž˜ ๋ ˆํฌ์ง€ํ„ฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค!!


์ œ๊ฐ€ ์ค€๋น„ํ•œ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ๊นŒ์ง€ ์ž…๋‹ˆ๋‹ค.
์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. :smile:

์ข‹์•„์š” 4

์ข‹์•„์š”, ๊ตฌ๋…, ์•Œ๋ฆผ์„ค์ •์€ ์‚ฌ๋ž‘์ž…๋‹ˆ๋‹ค.

์ข‹์•„์š” 2