SeriLog WPF

Markus Freitag 3,786 Reputation points
2020-07-12T16:55:23.937+00:00

Hi,
why ist the date not written?
Why green wavy line?

I need a logger for whole application, global Logger. Did I make it correct?
How I can start the console logger?
How I can start the console logger only in DEBUG mode?

Greetings Markus

public partial class MainWindow : Window  
    {  
        // https://github.com/serilog/serilog-sinks-rollingfile  
        // serilog analyser  
  
        //        install-Package Serilog  
        //        Install-Package Serilog.Sinks.RollingFile  
        //Install-Package Serilog.Sinks.ColoredConsole  
        //            Install-Package Serilog.Extensions.Logging  
        //            Install-Package Serilog.Sinks.File  
        //            Install-Package Serilog.Sinks.Console  
  
        //  https://marketplace.visualstudio.com/items?itemName=Suchiman.SerilogAnalyzer  
  
        //private static readonly ILogger Log;  
       // private static int I;  
  
        public MainWindow()  
        {  
            InitializeComponent();  
  
            Log.Logger = new LoggerConfiguration()  
                .MinimumLevel.Debug()  
                .WriteTo.Console()  
               // .WriteTo.RollingFile(@"c:\temp\Log-{Date}.txt")  
                .WriteTo.File(@"logs\Log-{Date}.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 100000)  
               // .WriteTo.File("logs\\myapp.txt", rollingInterval: RollingInterval.Day)  
                .CreateLogger();  
  
            Log.Information("Hello, world!");  
            //Log.Write("TEST write");  
              
  
            //var logger = new LoggerConfiguration()  
            //   .WriteTo.ColoredConsole()  
            //   .WriteTo.RollingFile(@"c:\temp\Log-{Date}.txt")  
            //   .CreateLogger();  
  
            //Log = logger;  
  
            int I = 0;  
            I++;  
            Log.Information($"Logger created {I}");  
  
            var appointment =  
                new { Id = 1, Subject = "Meeting of database migration", Timestamp = new DateTime(2020, 7, 11) };  
  
  
            Log.Information($"An appointment is booked successfully: {appointment.Id}");  
  
            Log.Error($"Failed #TEST# to book an appointment: {appointment.Timestamp}");  
        }  
  
        
        private void Button_Click_1(object sender, RoutedEventArgs e)  
        {  
            int i = 3;  
            Log.Information($"Logger created Button Click {i}");  
        }  

11923-serilog-config.jpg

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,691 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-07-13T03:41:44.823+00:00

    From the Filename format specifiers , you can see the form of the log file is fixed, so you may can't format it by your self. For your "Date" not working issue, you need see the following explanation:
    If you use Serilog.Sinks.File package in Nuget, you can use it like:

      Log.Logger = new LoggerConfiguration()
                        .MinimumLevel.Debug()
                        .WriteTo.Console()                
                        .WriteTo.File(@"logs\Log-.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 100000)
                       .CreateLogger();
    

    If you use Serilog.Sinks.RollingFile in Nuget, you can use it like:

     Log.Logger = new LoggerConfiguration()
                        .MinimumLevel.Debug()
                        .WriteTo.Console()
                        .WriteTo.RollingFile(@"logs\Log-{Date}.txt")
                        .CreateLogger();
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Markus Freitag 3,786 Reputation points
    2020-07-13T07:05:57.13+00:00

    Date is clear now, thanks.

    Why green wavy line?

    I need a logger for whole application, global Logger. Did I make it correct?
    How I can start the console logger?
    How I can start the console logger only in DEBUG mode?

    At moment no console logger is started, why? I use .WriteTo.Console()

    0 comments No comments