Serilog in WPF project for VS 2019

zleug 51 Reputation points
2020-10-25T03:45:48.887+00:00

Hi All.
I'm developing WPF .Net project in VS 2019 not .Net Core. I would like logging exceptions in this project. I have some questions about it:

  1. May I will add Serilog to that kind of application?
  2. What Serilog I need select to install? In VS in NuGet Packager Manager I found a bunch of Serilog applications.
  3. That is multi layers architecture project. In what layer Sirelog must to be installed? In Data or UI layer? Or any layer where will check exceptions?
  4. How to setup, configure and use Serilog?

Thanks.

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,760 questions
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-10-26T03:51:02.84+00:00

    I will show the steps to write a Serilog demo which target .Net framework:
    Step 1: Install the below three packages in your project Nuget: Serilog/Serilog.Sinks.Console/Serilog.Sinks.File
    Step 2: Add a Button in the UI
    Step 3: Add the code for the Button click event:

     private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                Log.Logger = new LoggerConfiguration()  
                        .MinimumLevel.Debug()  
                        .WriteTo.Console()  
                        .WriteTo.File("logs\\Log_SerilogDemoWPF.txt", rollingInterval: RollingInterval.Day)  
                        .CreateLogger();  
      
                Log.Information("Hello, This is my log!");  
      
                int a = 10, b = 0;  
                try  
                {  
                    Log.Debug("Dividing {A} by {B}", a, b);  
                    Console.WriteLine(a / b);  
                }  
                catch (Exception ex)  
                {  
                    Log.Error(ex, "Something went wrong");  
                }  
      
                Log.CloseAndFlush();  
            }  
    

    You need to install the packages where you want to write log.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Duane Arnold 3,221 Reputation points
    2020-10-26T05:47:40.37+00:00

    Why don't you keep it simple and use Log4Net?

    You should be using Global Exception Handling in the presentation layer/WPF project that catches the exception from all layers and logs the exception.

    You don't need a try/catch in any code in any layer, becuase the GEH will catch the exception thrown in any layer.

    https://www.wpf-tutorial.com/wpf-application/handling-exceptions/

    https://medium.com/nerd-stuff/logger-for-wpf-application-570eb9cbe546

    You should log the exception message, the stack trace and inner exception message if it's available/not null.

    I posted to wrong area the comments. But this is my answer post.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.