Serilog not writing to expected file

rutuja k 21 Reputation points
2021-01-20T07:38:50.057+00:00

I am new to Serilog. Need to understand below scenario:
Third party application invokes two different .net custom controls at the same time. Once invoked they run in background via dispatcher timer. The control can get loaded unloaded depending on user navigation on UI. These controls use common serilog config and are writing to different file paths(sink used is file).

In both the controls I have instantiated logger as below:
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings("customPrefix", configFilePath)
.CreateLogger();

Expectation is .net control A - writes in File A
control B - writes in File B

But the control B is writing some part in its file while some in file A. I am not sure why this is happening

Serilog config file sample:
Seciton A
<add key="customPrefix1:serilog:write-to:File.path" value="C:\AppLogs\AppA\myapp-{date}.txt" />

Section B
<add key="customPrefix2:serilog:write-to:File.path" value="C:\AppLogs\AppB\myapp-{date}.txt" />

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,797 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,626 Reputation points
    2021-01-21T03:35:21.937+00:00

    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:
    59471-capture.png


    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. rutuja k 21 Reputation points
    2021-01-31T14:22:40.377+00:00

    Thank you for the code. I did try it earlier it works fine when its an independent app.
    I was able to find why the original behaviour was happening, it was because:
    In the standalone application, these controls were called in a seq, and whichever control was getting called the last , was overriding the logger config.

    As per my understanding:
    Since .net controls are not independent app, they were invoked by main 3rd party app which was standalone. Ideally what i understood is, i shouldnt have had individual config for these controls. It should be only one for the app as a whole.

    Can you please guide on how can I close this question?


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.