You can use below workaround to let control A write logs in File A and control B write log in File B:
Step 1: Install Serilog.Sinks.RollingFile
to your project.
Step 2: Use below code to implement it:
The code for xaml:
<StackPanel>
<Button Width="120" Height="30" Content="Create Log A" Click="Button_ClickA"></Button>
<Button Width="120" Height="30" Content="Create Log B" Click="Button_ClickB"></Button>
</StackPanel>
The code for cs:
private void Button_ClickA(object sender, RoutedEventArgs e)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.WriteTo.RollingFile(@"E:\Logs\myapp-{Date}A.txt", retainedFileCountLimit: 10)
.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();
}
private void Button_ClickB(object sender, RoutedEventArgs e)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.WriteTo.RollingFile(@"E:\Logs\myapp-{Date}B.txt", retainedFileCountLimit: 10)
.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();
}
The reuslt picture is:
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.