vonhoff / Serilog.Sinks.RichTextBox.WinForms.Colored

A Serilog sink that writes log events to a WinForms RichTextBox control with colors and theme support.
Apache License 2.0
16 stars 8 forks source link

Configure template - Timestamp #11

Closed ADebla closed 2 months ago

ADebla commented 2 months ago

I am configure multiple richtextbox log with your package:

exemple for one :

` public static void ConfigureMainLogging(RichTextBox richTextBox) { var options = new RichTextBoxSinkOptions(ThemePresets.Dark, 200, 5, true, 10000); var richTextBoxSink = new RichTextBoxSink(richTextBox, options);

        GeneralLogger = new LoggerConfiguration()
            .MinimumLevel.Verbose()
            .WriteTo.File(
                path: $"{Constants.LoggingDirectoryPath}/log-.txt",
                rollingInterval: RollingInterval.Day,
                retainedFileCountLimit: 30,
                outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
            )
            .WriteTo.Sink(richTextBoxSink, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
            .CreateLogger();
    }`

But my template is not respected, I mean only in the file write. Is there a way to pass the template to the sink ?

Thanks for the help :D !

vonhoff commented 2 months ago

You can solve this by making use of the extension class as follows:

const string template = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.File(
        path: $"{Constants.LoggingDirectoryPath}/log-.txt",
        rollingInterval: RollingInterval.Day,
        retainedFileCountLimit: 30,
        outputTemplate: template
    )
    .WriteTo.RichTextBox(richTextBox1, maxLogLines: 10000, outputTemplate: template)
    .CreateLogger();

Let me know if this works for you.

vonhoff commented 2 months ago

I will close this issue as I assume it is solved. If it is not, please let me know.

ADebla commented 1 month ago

Hello,

Sorry for the late reply, I have been sick from my last message and did not work on the project until now.

Thanks for your solution, it does work.

vonhoff commented 1 month ago

Hello,

Sorry for the late reply, I have been sick from my last message and did not work on the project until now.

Thanks for your solution, it does work.

That's okay, thanks for the reply!