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.