Serilog.Sinks.ObservableCollection
1.0.0
dotnet add package Serilog.Sinks.ObservableCollection --version 1.0.0
NuGet\Install-Package Serilog.Sinks.ObservableCollection -Version 1.0.0
<PackageReference Include="Serilog.Sinks.ObservableCollection" Version="1.0.0" />
paket add Serilog.Sinks.ObservableCollection --version 1.0.0
#r "nuget: Serilog.Sinks.ObservableCollection, 1.0.0"
// Install Serilog.Sinks.ObservableCollection as a Cake Addin #addin nuget:?package=Serilog.Sinks.ObservableCollection&version=1.0.0 // Install Serilog.Sinks.ObservableCollection as a Cake Tool #tool nuget:?package=Serilog.Sinks.ObservableCollection&version=1.0.0
Serilog.Sinks.ObservableCollection
The ObservableCollectionSink
is a custom Serilog sink designed to log messages to an ObservableCollection
, which can be particularly useful in WPF applications where logs need to be displayed in the UI in real-time.
Prerequisites
- Serilog library installed in your .NET project.
- Access to a dispatcher (typically from a UI context in WPF or similar).
- Optional: Serilog.Sinks.PeriodicBatching for batch processing.
Installation
Ensure Serilog is installed in your project:
Install-Package Serilog
For batching capabilities, install the PeriodicBatching package:
Install-Package Serilog.Sinks.PeriodicBatching
Configuration
Configure the sink within your application's logging setup. Examples are provided for both standard and batched logging setups.
Standard Logging Configuration
var LogEvents = new ObservableCollection<LogEventViewModel>();
Log.Logger = new LoggerConfiguration()
.WriteTo.ObservableCollection(
LogEvents,
dispatcher: action => Application.Current.Dispatcher.Invoke,
configure: options =>
{
options.MaxStoredEvents = 1000; // Maximum log events to store
options.MinimumLevel = LogEventLevel.Information;
})
.CreateLogger();
Batched Logging Configuration
Enable batching by configuring the sink accordingly:
Log.Logger = new LoggerConfiguration()
.WriteTo.ObservableCollection(
LogEvents,
dispatcher: action => dispatcher.Invoke(action),
configure: options =>
{
options.EnableBatching = true;
options.BatchSizeLimit = 50;
options.Period = TimeSpan.FromSeconds(2);
options.MaxStoredEvents = 1000;
options.OnEmptyBatchAction = async () =>
{
// Custom action to perform when no events have been emitted
await Task.Run(() => Debug.WriteLine("No events emitted for the specified period."));
};
})
.CreateLogger();
Dependency Injection Configuration
_host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddSingleton<ObservableCollection<LogEvent>>();
// other services
})
.UseSerilog((context, services, configuration) =>
{
var logEvents = services.GetRequiredService<ObservableCollection<LogEventViewModel>>();
configuration
.MinimumLevel.Debug() // Set the global minimum level to Debug
.WriteTo.ObservableCollection(logEvents, Application.Current.Dispatcher.Invoke, options =>
{
options.MaxStoredEvents = 1000;
options.EnableBatching = true;
options.BatchSizeLimit = 50;
options.Period = TimeSpan.FromSeconds(2);
options.MinimumLevel = LogEventLevel.Information; // set the minimum level for the ObservableCollection sink to Information
options.OnEmptyBatchAction = async () =>
{
// Custom action to perform when no events have been emitted for a certain period of time
await Task.Run(() => Debug.WriteLine("No events emitted for the specified period."));
};
});
})
.Build();
Usage
Once configured, use the sink like any other Serilog sink:
Log.Information("This is a test log message.");
Examples of viewing logs in a WPF application are provided in the sample project.
Disposal
Remember to properly dispose of your loggers when the application is closing:
Log.CloseAndFlush();
This is especially important in batched mode to ensure all pending log messages are flushed to the UI.
Additional Notes
- Ensure the dispatcher is suitable for the thread managing the UI component to avoid cross-thread operations.
- MaxStoredEvents helps manage memory by limiting the number of log events stored. Adjust based on performance needs.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Serilog (>= 3.1.1)
- Serilog.Sinks.PeriodicBatching (>= 4.0.1)
-
net7.0
- Serilog (>= 3.1.1)
- Serilog.Sinks.PeriodicBatching (>= 4.0.1)
-
net8.0
- Serilog (>= 3.1.1)
- Serilog.Sinks.PeriodicBatching (>= 4.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 195 | 4/28/2024 |