CopyPastor

Detecting plagiarism made easy.

Score: 1.8393049240112305; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2021-09-08
by Tupac

Original Post

Original - Posted on 2021-09-02
by Zhi Lv



            
Present in both answers; Present only in the new answer; Present only in the old answer;

ou can use the Filter.ByExcluding() and Filter.ByIncludingOnly() to add filter expression to filter events passing through the Serilog pipeline, for each task or class, when you use them, you can set a property for them, then use the filter expression to filter the log and write to a different log file.
For demo purposes, we can set a property to the LogEvents in this method execution path.
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Worker is called"); using (LogContext.PushProperty("foobar", 1)) { Foo(); await Task.CompletedTask; } }
The following code snippet shows a filtering example.
const string logTemplate = @"{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u4}] [{SourceContext:l}] {Message:lj}{NewLine}{Exception}"; Log.Logger = new LoggerConfiguration() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Logger(l => { l.WriteTo.File("log.txt", LogEventLevel.Information, logTemplate, rollingInterval: RollingInterval.Day ); l.Filter.ByExcluding(e => e.Properties.ContainsKey("foobar")); }) .WriteTo.Logger(l => { l.WriteTo.File("foobar.txt", LogEventLevel.Information, logTemplate, rollingInterval: RollingInterval.Day ); l.Filter.ByIncludingOnly(e => e.Properties.ContainsKey("foobar")); }) .CreateLogger()
With the configuration above, normal logs (without the property foobar in log events) will be saved to the log.txt file, while logs with the property foobar will be saved to the foobar.txt file.
After running the application, the result as below: [![enter image description here][1]][1]

More detail information, check [this sample][2].

[1]: https://i.stack.imgur.com/4D63Q.gif [2]: https://github.com/dotnet-labs/SerilogFilterDemo
You can use the `Filter.ByExcluding()` and `Filter.ByIncludingOnly()` to add filter expression to filter events passing through the Serilog pipeline, for each task or class, when you use them, you can set a property for them, then use the filter expression to filter the log and write to a different log file.
For demo purposes, we can set a property to the LogEvents in this method execution path.
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Worker is called"); using (LogContext.PushProperty("foobar", 1)) { Foo(); await Task.CompletedTask; } }
The following code snippet shows a filtering example.
const string logTemplate = @"{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u4}] [{SourceContext:l}] {Message:lj}{NewLine}{Exception}"; Log.Logger = new LoggerConfiguration() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Logger(l => { l.WriteTo.File("log.txt", LogEventLevel.Information, logTemplate, rollingInterval: RollingInterval.Day ); l.Filter.ByExcluding(e => e.Properties.ContainsKey("foobar")); }) .WriteTo.Logger(l => { l.WriteTo.File("foobar.txt", LogEventLevel.Information, logTemplate, rollingInterval: RollingInterval.Day ); l.Filter.ByIncludingOnly(e => e.Properties.ContainsKey("foobar")); }) .CreateLogger()
With the configuration above, normal logs (without the property foobar in log events) will be saved to the log.txt file, while logs with the property foobar will be saved to the foobar.txt file.
After running the application, the result as below:
[![enter image description here][1]][1]
More detail information, check [this sample](https://github.com/dotnet-labs/SerilogFilterDemo).

[1]: https://i.stack.imgur.com/2pX1M.gif

        
Present in both answers; Present only in the new answer; Present only in the old answer;